404 Error on ASP.NET 4.5 on IIS 7

Microsoft

I recently tried deploying a new ASP.NET 4.5 MVC 3 application to our production web server. Everything worked fine on test and on my local machine, so I was not expecting trouble on production. But after I deployed it to production, every URL below the top-level immediately returned a 404 HTTP error. Other applications worked fine, but the new one gave me lots of trouble. This took a bit of work to figure out, but here's the answer.

The key is that my production server was Windows Server 2008 which runs IIS 7.0. The test server, it turns out, is running Windows Server 2008 R2 and IIS 7.5.

Rick Strahl ran into this a year ago and talked about the solution on his blog. It turns out that his solution applies in this case as well. The solution is to add the runAllManagedModulesForAllRequests key to the web.config:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
    </modules>
 </system.webServer>

That indeed was the answer. After I made that change to the release web.config and republished, the app worked as expected. But I wondered how it applied in this case, given that he talked about ASP.NET 4.0 and this was ASP.NET 4.5.

It turns out that the ASP.NET Team talked about this in their What's New in ASP.NET 4.5 and Visual Studio 11 Beta document. In it, they describe how the web project templates in Visual Studio 2012 RC (VS 11) do not include that key by default.

Note that there are some caveats to doing this and you'll want to read the document to decide for yourself. In my case, I need to get the test and production environments in sync so that problems like this get caught in test.

Don't forget to follow me on Twitter @daveiffland.