We have an existing application which has been configured to use AutoFac with ASP.Net MVC:
1) All the controllers, action info and routings have been registered manually using AutoFac 2) Controllers are resolved with an implementation of IControllerFactory's (AutoFacControllerFactory) CreateController() method 3) I have added a new API controller to the existing application 4) Web API routings have been added before any traditional ASP.Net routings as follows:
GlobalConfiguration.Configuration.Routes.MapHttpRoute("GetNetworkFeatures",
"secure/banking/administration/GetNetworkFeatures/Get",
new { controller = "GetNetworkFeatures" });
5) New API controllers have been registered using AutoFac as well before containerBuilder.Build() si called as follows:
foreach (var module in modules)
{
builder.RegisterApiControllers(module);
}
container = containerBuilder.Build();
GetNetworkFeaturesController is loaded in one of the above modules
6) Both dependency resolvers have been registered as follows:
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
7) .Net version is 4.5.2, we are using ASP.Net MVC version 3, AutoFac version is 2.6.3.862, AutoFac.Integration.MVC version is 2.6.3.862, AutoFac.Integration.WebApi version is 2.6.3.863, and Newtonsoft.Json version is 11.0.0.0, IIS server version is 6.1 in Windows Server 2008
8) The target is to make Autofac, Asp.net MVC routing and Web API routing coexist in the same application pool, when I try web API endpoint http://localhost:7070/secure/banking/administration/GetNetworkFeatures/Get, it still goes to AutoFacControllerFactory's CreateController() method, instead of routing to Get() method of Web API controller GetNetworkFeaturesController
9) I think this may be related to , so I have changed it to true, but the problem still persists. Basically it looks like configured Web API routing is not respected, although it has no compilation and build errors, if I removed the following default route, it starts to throw HTTP 403 error:
RouteTable.Routes.MapRoute("Default", "{*url}", new { controller = "Unspecified", action = "Unspecified" });
10) The following threads have been referenced:
https://stackoverflow.com/questions/11484904/is-it-possible-to-configure-autofac-to-work-with-asp-net-mvc-and-asp-net-web-api
https://stackoverflow.com/questions/37069666/autofac-with-web-api-controller
https://stackoverflow.com/questions/44986001/web-api-route-values-is-empty-on-action-executes-filter
https://stackoverflow.com/questions/15556035/all-asp-net-web-api-controllers-return-404
https://www.aspforums.net/Threads/218940/Solved-URL-Routing-not-working-with-Web-API-in-IIS-server-and-ASPNet-MVC/