CustomErrors not working in .NET

CustomErrors not Working in .Net

CustomErrors not working in .NET

I was recently working on an ASP.NET website and I came across a seemingly common problem… customErrors not working in .NET.

The problem arises when developing in Visual Studio. CustomErrors work fine here but when you push to a development server running IIS7 they stop working. This is because IIS7 favours httpErrors.

 

How to fix CustomErrors not working in .NET

The only way that worked perfectly for me was to turn off customErrors and replace IIS’s error pages via the Web.Config. This will send the correct status code with the response and has the benefit of not going through the MVC.

First you will need to turn off customErrors (in system.web) of Web.Config.

<customErrors mode="Off" />

Then add the httpErrors to the system.webserver section of the Web.Config.

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="404" path="Error404.html" responseMode="File" />
  <error statusCode="500" path="Error.html" responseMode="File" />
</httpErrors>

This will remove the IIS default error pages and replace with your custom error pages instead. You should use responseMode=”file” if the url is a direct link to a file or use executeUrl if it’s a url.

Steven McComb

No Comments

Reply