peflorencio
peflorencio

Reputation: 2482

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

Upvotes: 4

Views: 3356

Answers (4)

Grégoire GUYON
Grégoire GUYON

Reputation: 41

This work for me in owin startup class.

// instead of : app.UseWebApi(GlobalConfiguration.Configuration)    
app.UseWebApi(new HttpConfiguration());

Upvotes: 0

XperiAndri
XperiAndri

Reputation: 1208

In my case I had to move all the configuration from Application_Start in post above to Startup.Configuration replacing app.UseWebApi(config);

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239430

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.

Upvotes: 6

Yashasvi
Yashasvi

Reputation: 207

Try adding this code in your RouteConfig.cs file.

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

Upvotes: 0

Related Questions