← All articles
development

ASPNET MVC IIS7 and Bad Request

Custom 404 handling working in Cassini but IIS7 returns Bad Request instead? Unlock the httpErrors config section with appcmd to fix it.

Donn Felker

Donn Felker

2009.05.01 · 1 min read · updated July 5, 2013

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.

Donn Felker

Written by Donn Felker

I've spent 25+ years shipping software, writing books, and running my own companies. I write about thinking clearly, working for yourself, and doing real work while AI rewrites the rules. New essays land here every week.

Get the newsletter

Keep reading