WebApi and ASP.NET 4 routing issues

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Sometimes when deploying WebApi application in production system, you might figure out that the application does not work. In my case this is almost always the case :(.
One of my favorite errors I get when trying to invoke some remote operation (ASP net guys call it ‘action’) is following one:

”HTTP Error 404.0 - Not Found The resource you are looking for has been removed.. bla, bla”


To understand this, you have to know that there is a HttpModuleUrlRoutingModule-4.0’, which is responsible for routing of requests inside of WebApi and MVC.
For example If you send some request to URL like  host/path/controller/action, this module will route it to WebApi.
Assuming that this module is not activated for site, such requests will be automatically handled by IIS and will end with error “404 not found”. By knowing this, we can assume
that 404 is related to configuration issue of this module.

In the host configuration file of IIS this module is registered as any other module. Following line shows original registration:

<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="managedHandler,runtimeVersionv4.0" />

Please note the attribute preCondition, which basically says that the module will be executed for all managed handlers (aspx etc.). More about conditions can be found here:
http://www.iis.net/learn/get-started/introduction-to-iis/iis-modules-overview#Precondition


Because WebApi requests are not typical managed requests this module will ignore such requests according to configuration entry shown above. To enforce it to be execute you can
ad following to your web.config:

   <modules runAllManagedModulesForAllRequests="true"  />
   

This will force all modules to be executed on all requests. I would recommend putting this line in a case of debugging when you don’t know which module should execute, but it is not executed.
If all after that works as expected try to figure out which module is responsible for processing of your request. Please note that leaving this line in configuration is not very smart solution, because
this can cause other modules to work properly or it can degrade the performance, by running unnecessary modules on all requests.

Once you know which module should be executed, you can explicitly define the condition. Following example shows how this should be done in a case of UrlRoutingModule-4.0.

  <modules runAllManagedModulesForAllRequests="true" >
       <remove name="UrlRoutingModule-4.0" />
       <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  </modules>

By putting preConfition on “”, you will enforce all kind of requests to be routed to this module. 


Posted Oct 07 2012, 10:15 PM by Damir Dobric
Filed under: ,

Comments

ASP.NET Web API not working on Azure - Windows Azure Blog wrote ASP.NET Web API not working on Azure - Windows Azure Blog
on 12-18-2013 23:28

Pingback from  ASP.NET Web API not working on Azure - Windows Azure Blog

ASP.NET Web API not working on Azure - Windows Azure Blog wrote ASP.NET Web API not working on Azure - Windows Azure Blog
on 12-18-2013 23:28

Pingback from  ASP.NET Web API not working on Azure - Windows Azure Blog

ASP.NET Web API not working on Azure - Windows Azure Blog wrote ASP.NET Web API not working on Azure - Windows Azure Blog
on 12-18-2013 23:29

Pingback from  ASP.NET Web API not working on Azure - Windows Azure Blog

ASP.NET Web API not working on Azure | Developers Questions - Msn4Free.com wrote ASP.NET Web API not working on Azure | Developers Questions - Msn4Free.com
on 12-18-2013 23:39

Pingback from  ASP.NET Web API not working on Azure | Developers Questions - Msn4Free.com

developers.de is a .Net Community Blog powered by daenet GmbH.