不正确的url,如http://localhost:54321/foo/bar并且像CCD_ 1这样的错误在我的本地主机中处理得很好。在部署到IIS 7.5之后,它在上述两种情况下都会返回一个空白页面。我在这里看到过其他人有这个问题(这是莫尔德的评论,获得了几张赞成票)。代码:
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>
错误控制器
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 }
);
未找到
<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
上仍有空白页
在我的案例中,原因是IIS-web设置-请求筛选-SQL注入筛选器。。。
如果url或查询字符串包含;
,则响应以状态404.19结束-筛选规则拒绝。然后显示空白页。IIS重定向到自定义错误,如下所示http://server.com/Error404.aspx?404;http://server.com/missingURL而字符;
就是请求过滤器因为SQL注入而终止请求的原因。