Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI + MVC + Owin: MVC routes don't work

I have an WebAPI 2 app with OWIN. Now I'm trying to add an MVC 5 controller to everything, but my MVC routes are not being found. I'm receiving the following error:

No HTTP resource was found that matches the request URI 'https://localhost:44320/home'.

No type was found that matches the controller named 'home'.

The controller's name is correct (HomeController), it is public, and I'm configuring the routes in Global.asax:

protected void Application_Start()
{
    HttpConfiguration config = GlobalConfiguration.Configuration;
    ModelBinderConfig.RegisterModelBinders(config.Services);
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

I also have the OWIN Startup class:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = GlobalConfiguration.Configuration;

        var globalExceptionHandler = GetGlobalExceptionHandlerConfiguration();
        config.Services.Replace(typeof(IExceptionHandler), globalExceptionHandler);
        app.UseWebApi(config);
    }
}

I've noticed that when I comment out the app.UseWebApi(config) line, the MVC routes start to work again.

Does anyone know what is going on and how to solve this?

Thanks

like image 398
peflorencio Avatar asked Sep 16 '25 10:09

peflorencio


1 Answers

The app.UseWebApi() command is for self-hosting with OWIN, which is incompatible with MVC, so you can only use it when your project is exclusively a Web Api project. Just remove that, and you're golden.

like image 168
Chris Pratt Avatar answered Sep 18 '25 09:09

Chris Pratt