http://localhost:54321/foo/bar等不正确的网址以及HttpNotFound()
等错误在我的localhost中处理没有问题。部署到IIS 7.5后,它会在上述两种方案中返回空白页。我已经看到其他人有这个问题Here(这是Moulde的评论,有几个赞成票)。代码:
的Web.Config
<system.web>
<customErrors mode="Off" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/Error/NotFound"/>
</customErrors>
</system.web>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound"/>
<remove statusCode="500" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error/Error"/>
</httpErrors>
ErrorController
public class ErrorController : Controller
{
public ActionResult NotFound() {
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View();
}
}
RouteConfig
routes.MapRoute(
"Error - 404",
"NotFound",
new { controller = "Error", action = "NotFound" }
);
routes.MapRoute(
"Error - 500",
"Error",
new { controller = "Error", action = "Error" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
NOTFOUND
<h2>404 Not Found</h2>
<div class="alert alert-info">
<p><strong>Whoops...</strong></p>
<p>The resource you were looking for doesn't exist</p>
</div>
服务器运行Windows Server 2008 R2。当我转到... / foo / bar或某个操作返回HttpNotFound()
时,它会返回一个空白页面。任何的想法?
主/测试
public class HomeController: Controller{
// ...
public ActionResult Test(){
return HttpNotFound();
}
}
更新
当我转到服务器上的/Error/NotFound
时,它返回一个空白页面以及404.与Fiddler的回复:
HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B? QzpccWhlXFByb2plY3RzXFZlbmRvclBvcnRhbFxWZW5kb3JQb3J0YWxcRXJyb3JcTm90Rm91bmQ=?=
X-Powered-By: ASP.NET
Date: Thu, 23 Apr 2015 13:24:49 GMT
Content-Length: 5434
更新2:
如果我离开existingResponse="Replace"
,我可以在服务器上打开Error / NotFound。但仍然在MyServer/MyProject/foo/bar
答案 0 :(得分:0)
在我的情况下,原因是在IIS中 - 网页设置 - 请求过滤 - SQL注入过滤器...
如果url或查询字符串包含;
,则响应以状态404.19结束 - 通过过滤规则拒绝。然后显示空白页面。 IIS重定向到此http://server.com/Error404.aspx?404;http://server.com/missingURL之类的自定义错误,而char ;
是请求筛选器因SQL注入而终止您的请求的原因。