Why is my 404 custom error handler not working after deploying to a web server

I followed this post and created a global error handler. And I added 404 to handle the error, however it works fine when I test locally, but after deploying it to the web server, my user message is no longer displayed. Instead, an ugly default error appears.

In remote debugging, I can track progress, and it really falls into my usual 404-dimensional action, but somehow IIS took up at some point.

In my Global.asax.cs file, I have:

protected void Application_Error() { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Error"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } IController errorController = new ErrorController(); var rc = new RequestContext(new HttpContextWrapper(Context), routeData); errorController.Execute(rc); } 

then in my ErrorHandler.cs I:

 public ActionResult General(Exception exception) { // log error return Content("General error", "text/html"); } public ActionResult Http403(Exception exception) { return Content("Forbidden", "text/plain"); } public ActionResult Http404(Exception exception) { return Content("Page not found.", "text/plain"); // this displays when tested locally, but not after deployed to web server. } 

}

+4
source share
1 answer

You are correct, remote IIS uses your 404 pages. You need to tell IIS to skip custom error settings. Response.TrySkipIisCustomErrors = true;

So your code should look like this.

 protected void Application_Error() { //... Response.TrySkipIisCustomErrors = true; Response.StatusCode = 404; //...rest of your code } 

Also check this link for more information http://www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors

+8
source

All Articles