Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding WebAPI as a child/nested application in IIS

Steps to recreate this issue:

  1. Inside IIS create a new .net 4 website (herein known as the parent). Drop a test image in the folder for this website and observe you can request it successfully in a browser
  2. Add a new virtual directory OR application under the parent that points to a WebAPI 2 project
  3. Attempt to access the API in the browser using www.path-to-parent-site.com/api/something/or/other

I get the following error:

 System.Web.Routing.UrlRoutingModule does not implement IHttpHandlerFactory or IHttpHandler.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Configuration.ConfigurationErrorsException: System.Web.Routing.UrlRoutingModule does not implement IHttpHandlerFactory or IHttpHandler.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ConfigurationErrorsException: System.Web.Routing.UrlRoutingModule does not implement IHttpHandlerFactory or IHttpHandler.]
   System.Web.Configuration.HandlerFactoryCache.GetHandlerType(String type) +12328272
   System.Web.Configuration.HandlerFactoryCache..ctor(String type) +27
   System.Web.HttpApplication.GetFactory(String type) +94
   System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +375
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34209 

Is there anything I can do to get this working? I can find very little relevant information on this particular issue and even fewer solutions to those who have seen this error.

Note: If I add the WebAPI 2 project as a new website in IIS it works perfectly; its only when its nested as a child (either virtual directory or application has the same problem) that this happens.

Thanks

like image 346
LDJ Avatar asked Jan 08 '15 07:01

LDJ


1 Answers

WebApi is not supposed to be hosted on a virtual directory, if you wanna do so you need to make the routing pattern dynamic and load the first part from the virtual directory.

var virtualDirectory = request.ApplicationPath;
routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: virtualDirectory + "/api/{controller}/{id}",
    defaults: new {
        id = RouteParameter.Optional
    }
);
like image 179
akardon Avatar answered Sep 29 '22 19:09

akardon