Closed MoonStorm closed 9 years ago
It turned out WebApi was not configured correctly in an MVC + WebApi combo. The key is to use a different HttpConfiguration
than the global one:
// initialize web pages & mvc routing first
AreaRegistration.RegisterAllAreas();
ConfigureRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// set up IOC and configure authentication
app.UseNinjectMiddleware(CreateKernel);
ConfigureAuth(app);
// finally the web api, on its own http configuration
var webApiConfig = new HttpConfiguration();
webApiConfig.MapHttpAttributeRoutes();
webApiConfig.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate:api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
app.UseNinjectWebApi(webApiConfig);
// ensure all the configs are ready
GlobalConfiguration.Configuration.EnsureInitialized();
webApiConfig.EnsureInitialized();
The problem can be easy to reproduce by creating a new MVC+WebAPI project via the standard ASP.NET project template, adding the required Owin and the Ninject packages, removing the setup logic in the
global.asax.cs
, creating aStartup
class as the one below:and by changing the default GET of the
ValuesController
to:When
UseNinjectWebApi
is active in theStartup
, the GET will fail with aNotImplementedException
.When
UseNinjectWebApi
is not active, the GET will succeed. This is becauseHostedHttpRouteCollection.GetVirtualPath
is looking for the HTTP context in the property bag of the request. This is set up by theHttpControllerHandler
which attaches the context to theMS_HttpContext
key.