I’ve created a ASP.NET MVC 3 application with custom error pages for HTTP status code 404 (“Not Found”) and 500 (“Internal Server Error”). I registered my custom pages in the Web.config as follows:
After deploying my ASP.NET application to an IIS 7.5 web server I was surprised by the fact that the IIS web server seems to override my error page configuration: Instead of showing my (custom) error pages the web server returns the default IIS error pages. After some research I found out, that by default the IIS “overrides” the HTTP response with its default error pages if one returns an HTTP (error) status code (like 404 or 500). One solution to disable this behavior is to set Response.TrySkipIisCustomErrors in the error controller:
[HandleError]
public class ErrorController : Controller
{
public ActionResult NotFound()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View("NotFound");
}
public ActionResult InternalServerError()
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View("InternalServerError");
}
}
10 responses to “ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages”
Thanks. Your article really helped me. For me also, the custom error pages worked fine locally on the Cassini server, but as soon as I deployed the site to IIS 7.5, it was overrideing the custom error pages with its default error pages on returning an HTTP (error) status code (like 404 or 500)(good practice for a proper response code and SEO)…just out of curiosity you mentioned in the article that “One solution to disable this behavior is to set Response.TrySkipIisCustomErrors in the error controller”, is there any other solution other than this one. would be nice to know/learn if any other solution exists. Also, what are some other error codes that should be put in the customErrors section apart from 404 and 500 as a best practice
Again thanks for the excellent post and solution.
Hi jatin,
another solution for IIS 7+ is to use the <httpErrors> tag within <system.webServer> that defines the error page behavior not per page/request but globally for the application in your web.config as follows:
You can find more information about the <httpErrors> tag here.
IMHO, for SEO and user experience one should define custom error pages for all error codes that could occur. For example, it is meaningful to have a custom 401 error (“Unauthorized”) page in web applications that uses authentication.
It bothers me that you get a 302 first before to get to the real error page (404 or 500).
In Webforms you can use the redirectMode=”ResponseRewrite” option, but this is ignored in mvc.
[…] ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages | Jan Jonas… […]
INMHO, I think using httpErrors element in web.config to replace IIS default is easier and better ex:
and turn off asp.net custom errors;
More info;
http://tipila.com/tips/use-custom-error-pages-aspnet-mvc
[…] is the catch: The Windows 2008 server is running the latest IIS 7.5, and this version will by default “override” the HTTP response with its default error pages if […]
I’ve tried a million different ways, but I see the default iis page when I force a 404. Not sure what I’m doing wrong.
Working on the web.config located in root directory.
under system.web:
———————
under system.webserver:
—————————-
[…] is the catch: The Windows 2008 server is running the latest IIS 7.5, and this version will by default “override” the HTTP response with its default error pages if […]
Wasted considerable time in trying to do such a simple task: redirect a 500 error to a custom page. And this is the only approach that worked (and the most neat and clean one I came across).
Congratulations.
nice post.it saved lots of time.