Not a subscriber?

Join thousands of others who are building self-directed lives through creativity, grit, and digital strategy—breaking free from the 9–5.
Receive one free message a week

ASPNET MVC IIS7 and Bad Request

While working on an app recently I ran into the issue of needing to handle httpErrors at runtime in a dynamic fashion. I did this with some custom controller execution and routing.

If a user were to type in an invalid address a custom 404 would be returned.

Example: http://example.org/foo

The Problem

Unfortunately my custom error handling code was working in Cassini but in IIS7 it was not.

IIS 7 would return “Bad Request”. Here is how I fixed it.

The problem was that I was not telling IIS7 what to do with httpErrors.

The Fix

You can enable this by doing so in the web.config:

<system.webServer>

  <httpErrors errorMode=”Detailed” />

</system.webserver>

To use this option you will need to unlock this config section for IIS7. To do this, open the command prompt on the IIS7 box and execute the following command:

%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/httpErrors

or if you want to unlock the config section for just a particular site, execute this:

%windir%\system32\inetsrv\appcmd.exe unlock config "SiteName/app/url" -section:system.webServer/httpErrors

I was now able to handle the http errors returned from IIS7.