Donn Felker

Not A Subscriber?

Join thousands of people who build and refuse to get left behind. One message a week on building, using AI as an accelerator, and staying sharp in the new AI frontier.

Receive one free message a week

May 1, 2009 Donn Felker · updated July 5, 2013

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>

 

</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.