Contact   •   Products   •   Search

Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs

ASP.NET Routing not working on IIS 7.0


I ran into a nasty little problem today when deploying an application using ASP.NET 4.0 Routing to my live server. The application and its Routing were working just fine on my dev machine (Windows 7 and IIS 7.5), but when I deployed (Windows 2008 R1 and IIS 7.0) Routing would just not work. Every time I hit a routed url IIS would just throw up a 404 error:

IISErrorDisplay

This is an IIS error, not an ASP.NET error so this doesn’t actually come from ASP.NET’s routing engine but from IIS’s handling of expressionless URLs. Note that it’s clearly falling through all the way to the StaticFile handler which is the last handler to fire in the typical IIS handler list. In other words IIS is trying to parse the extension less URL and not firing it into ASP.NET but failing.

As I mentioned on my local machine this all worked fine and to make sure local and live setups match I re-copied my Web.config, double checked handler mappings in IIS and re-copied the actual application assemblies to the server. It all looked exactly matched. However no workey on the server with IIS 7.0!!!

Finally, totally by chance, I remembered the runAllManagedModulesForAllRequests attribute flag on the modules key in web.config and set it to true:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="ScriptCompressionModule" type="Westwind.Web.ScriptCompressionModule,Westwind.Web" />
    </modules>
  </system.webServer> 

And lo and behold, Routing started working on the live server and IIS 7.0!

This seems really obvious now of course, but the really tricky thing about this is that on IIS 7.5 this key is not necessary. So on my Windows 7 machine ASP.NET Routing was working just fine without the key set. However on IIS 7.0 on my live server the same missing setting was not working. On IIS 7.0 this key must be present or Routing will not work.

Oddly on IIS 7.5 it appears that you can’t even turn off the behavior – setting runtAllManagedModuleForAllRequests="false" had no effect at all and Routing continued to work just fine even with the flag set to false, which is NOT what I would have expected.

Kind of disappointing too that Windows Server 2008 (R1) can’t be upgraded to IIS 7.5. It sure seems like that should have been possible since the OS server core changes in R2 are pretty minor. For the future I really hope Microsoft will allow updating IIS versions without tying them explicitly to the OS. It looks like that with the release of IIS Express Microsoft has taken some steps to untie some of those tight OS links from IIS. Let’s hope that’s the case for the future – it sure is nice to run the same IIS version on dev and live boxes, but upgrading live servers is too big a deal to do just because an updated OS release came out.

Moral of the story – never assume that your dev setup will work as is on the live setup. It took me forever to figure this out because I assumed that because my web.config on the local machine was fine and working and I copied all relevant web.config data to the server it can’t be the configuration settings. I was looking everywhere but in the .config file forever before getting desperate and remembering the flag when I accidentally checked the intellisense settings in the modules key.

Never assume anything. The other moral is: Try to keep your dev machine and server OS’s in sync whenever possible. Maybe it’s time to upgrade to Windows Server 2008 R2 after all.

More info on Extensionless URLs in IIS

Want to find out more exactly on how extensionless Urls work on IIS 7? The check out  How ASP.NET MVC Routing Works and its Impact on the Performance of Static Requests which goes into great detail on the complexities of the process. Thanks to Jeff Graves for pointing me at this article – a great linked reference for this topic!

Make Donation
Posted in IIS7  Windows  


Feedback for this Post

 
# re: ASP.NET Routing not working on IIS 7.0
by Pranav March 28, 2011 @ 5:55am
Hi Rick,

I have got same problem on server. Probably this was because you are using MVC/ Routing in ASP.NET 4.0.
My problem was solved mere by changing Appllication pool to ASP.NET 4.0 for that website/application & restarting site on IIS.

Hope it can work at other side too.
# re: ASP.NET Routing not working on IIS 7.0
by Kev March 28, 2011 @ 9:51am
You should also be aware of this issue on IIS7.5 which I reported to PSS what seems like a month of Sundays ago (which eventually triggered this KB article):

http://support.microsoft.com/kb/2520479

As a web hoster this really buggered things up for us and is still unresolved :(.
# re: ASP.NET Routing not working on IIS 7.0
by Bob Miner April 22, 2011 @ 12:20pm
THANKS RICK! I had a web site built with an early version of ASP.NET MVC 3 and the web.config files didn't have this entry. Adding it fixed the problem. It was odd though that the problem didn't show up until a recent Windows Update. Thanks again.
# re: ASP.NET Routing not working on IIS 7.0
by txcraig May 31, 2011 @ 8:54am
Hi Rick. I wanted to comment on this statement in bold above:
"On IIS 7.0 this key must be present or Routing will not work"

We found that this setting is not required to get routing working in Server 2008 IIS 7.0. Originally we did have runAllManagedModulesForAllRequests="true" and Routing was working, then we found this post about why using that setting is not a good idea:

http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html

We found that if we applied KB980368 (requires Windows Server 2008 SP2) we could get Routing to work without relying on runAllManagedModulesForAllRequests.
# re: ASP.NET Routing not working on IIS 7.0
by Rick Strahl May 31, 2011 @ 11:27am
@txCraig - Part of the the issue is that this behavior has changed many times. Original IIS 7 RTM in integrated routed all requests through managed code. Then it was turned off in an update, then the configuration switch was changed in IIS 7.5. Now there's a KB and patch that give yet more options.

Unless your site(s) are super critical in terms of resource usage, I'd say the overhead of having all requests going through the module pipeline is not that critical. Other than initial startup slowness for an ApplicationPool and first access performance is not likely to have more than a fractional percentage impact. I suppose it depends on your app and what module filtering occurs on requests, but if we're talking only about the routing module (and default ASP.NET pipeline modules) the overhead is really small. Plus IIS caching will override for a lot of requests served from disk.
# re: ASP.NET Routing not working on IIS 7.0
by Suman September 03, 2011 @ 2:00am
you can try by adding a new httphandler mapping on the specific website in IIS 7.5 for your desired choice of path format. Make sure to put this new entry at a proper index in the ordered list of handler entries.
# re: ASP.NET Routing not working on IIS 7.0
by James McCormack January 27, 2012 @ 9:20am
txcraig - You rock, we couldn't find information about that KB hotfix anywhere! Solved our MVC 3 problem with 404 errors on Windows Server 2008 (non R2).
# re: ASP.NET Routing not working on IIS 7.0
by Paul August 07, 2012 @ 8:48am
Thanks Rick.
You da man! I was trying to figure out the same thing. I assumed that the 2008 was running 7.5 as well and all was identical, but apparently not.
# re: ASP.NET Routing not working on IIS 7.0
by gilles September 25, 2012 @ 3:32am
I got the same trouble today on IIS7.0 / not affecting IIS7.5.
Found the following KB: http://support.microsoft.com/kb/980368
I installed the fix in the KB (which requires an OS restart).
It fixed the problem for us.
Gilles
# re: ASP.NET Routing not working on IIS 7.0
by Tim October 04, 2012 @ 12:09am
When all else fails, this also occurs when adding a .NET 4.0 MVC "app" under a .NET 2.0 parent Web Site (ex. "Default Web Site"). There doesn't seem to be a good way to run a mix of 2.0/3.5/4.0 sites under different top-level parent sites. For example, if your top-level site is 2.0, then 4.0 apps break. If your top-level is 4.0, then 2.0 apps under that site break.
# re: ASP.NET Routing not working on IIS 7.0
by nl-x February 20, 2013 @ 6:45am
This also drove me crazy yesterday. And with me it was the other way around. Worked well on Windows Server 2008. But failed to work on Windows 7. Both IIS 7.5.

I think it's because on Windows Server 2008 (my development machine), Visual Studio already applies the patch or does something else that makes it work. On the Windows 7 machine that did NOT have Visual Studio installed, it failed to work, untill we added the line in Web.config.
# re: ASP.NET Routing not working on IIS 7.0
by Hitesh Gaur July 17, 2013 @ 9:34pm
I am facing the same problem even though i have set the key to "runAllManagedModulesForAllRequests" to true. I have 73 routes to load but problem is that RegisterRoutes function is never being called by when application start. i have created two log files one from development environment and other from live environment
# re: ASP.NET Routing not working on IIS 7.0
by phil August 01, 2013 @ 1:55am
We have test and deployed customer systems with Windows Server 2008 R2 Enterprise version 6.1 build 7601 SP1, IIS 7.5.* all of which worked fine with bundling.

We had one customer with Windows Server 2008 Standard version 6.0 build 6002 SP2, IIS 7.0.* which 404ed on all requests for bundled files.

We concluded that the problem was IIS7.0. The KB patch refused to install - said it wasn't needed, so we tried your fix and that works perfectly.
# re: ASP.NET Routing not working on IIS 7.0
by Rick Strahl August 01, 2013 @ 2:55am
@Phil - they might have rolled that into one of the .NET Framework updates since.
# re: ASP.NET Routing not working on IIS 7.0
by Todd July 09, 2014 @ 12:18pm
This is a great thread going!

@txcraig and others let us to a working solution.

Adding the specific module needed worked for us. We had the "evil" runAllManagedModulesForAllRequests="true" attribute, but didn't want to use it because of the performance concerns (and should you just run all really?). Simply adding the correct module, and removing that evil attribute solved the problem.

We didn't install the patch for Windows 2008 SP2, IIS 7.0 we wanted to try to work around that.

<modules>
<remove name="BundleModule" />
<add name="BundleModule" type="System.Web.Optimization.BundleModule" />
</modules>
# re: ASP.NET Routing not working on IIS 7.0
by Ashish Thakur October 25, 2014 @ 12:54pm
Thanks Rick for posting such good articles,

I am able to perform GET and POST request through WebAPI on LocalHost development server via AJAX .

After deployment to IIS server on AWS I am not at all able to perform GET and POST through AJAX.( I have referred your articles related to WebAPI and ASP.Net passing multiple parameters and like others)

I have noticed following points after deployment to IIS Server.
1) when I typed URL for WebAPI I am able to get JSON data as expected from AWS.
2) AJAX returning error on POST and GET request
3) Fiddler returns JSON data as expected for GET

Is it related to ASP.Net Routing ?
Appreciate your time and guidance.
 


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2014