Blank custom 404 page after deploying to IIS 7.5

2019-07-21 03:42发布

问题:

Incorrect urls like http://localhost:54321/foo/bar and errors like HttpNotFound() are handled in my localhost without problems. After deploying to IIS 7.5, it return a blank page on both of the above scenarios. I've seen someone else had this problem Here(It was the comment from Moulde with several upvotes). The code:

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>

The server runs Windows Server 2008 R2. When I go to .../foo/bar or an action returns HttpNotFound(), it returns a blank page. Any idea?

Home/Test

public class HomeController: Controller{
    // ...
    public ActionResult Test(){
        return HttpNotFound();
    }
}

Update: When I go to /Error/NotFound on server, it returns a blank page as well with 404. The response with 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

Update 2: If I take existingResponse="Replace" away, I can open Error/NotFound on the server. But still got blank page on MyServer/MyProject/foo/bar

回答1:

In my case the reason was in IIS - web settings - Request Filtering - SQL injection filter...

If url or query string contains ;, response ends with status 404.19 - Denied by filtering rule. Then blank page is displayed. IIS redirect to custom error like this http://server.com/Error404.aspx?404;http://server.com/missingURL and the char ; is why Request Filter end your request because of SQL injection.