ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages

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:

<configuration>
  <system.web>
    <customErrors defaultRedirect="/Error/InternalServerError" mode="RemoteOnly">
      <error redirect="/Error/NotFound" statusCode="404" />
      <error redirect="/Error/InternalServerError" statusCode="500" />
    </customErrors>
  </system.web>
</configuration>

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");
  }
}

Comments

  1. jatin

    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.

  2. Jan Jonas

    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:

    <system.webServer>
      <httpErrors errorMode="Custom"/>
      [...]
    </system.webServer>

    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.

  3. David De Sloovere

    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.

  4. amola

    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

  5. michael

    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:
    —————————-

  6. Avraham

    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.

  7. Nitin

    nice post.it saved lots of time.

Leave a comment

Your email address will not be published. Required fields are marked *

*