MVC 5, Web API And Owin

10,240

Solution 1

Am I correct when I say, Web API can run on OWIN and MVC 5 cannot?

Not clear what you're asking but OWIN is not server but it's a middleware which help injecting pipelines in order to pre-process requests in stages and it doesn't depend on WebAPI or MVC version but it depends if hosting server have OWIN specifications implemented.

Is RouteConfig.RegisterRoutes(RouteTable.Routes) enough?

yes this will work for Asp.net MVC but for web api you need to register the routes in separate configuration class. Usually the WebAPI configuration may look like as specified in default Asp.net WebAPI template(> vs2013)

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

Make sure the Url you're requesting matches either the MVC or WebAPI route templates.

Solution 2

Yes, you're right, MVC 5 (based in ASP.NET 4) requires IIS, it can't self-host. MVC 6 (based in ASP.NET 5, now called ASP.NET Core 1) doesn't have this limitation. If you need self-hosting, start playing with ASP.NET Core 1 (it is incredibly awesome) or if you need RTM right now, use WebAPI.

Share:
10,240
Steven Yates
Author by

Steven Yates

Self confessed geek

Updated on June 14, 2022

Comments

  • Steven Yates
    Steven Yates almost 2 years

    Am I correct when I say, Web API can run on OWIN and MVC 5 cannot?

    So in my project i still need my Global.asax with public class WebApiApplication : System.Web.HttpApplication

    At the moment I have my owin Startup.cs which looks like this:

    public void Configuration(IAppBuilder app)
    {
        var httpConfig = new HttpConfiguration
        {
    
        };
    
    
        WebApiConfig.Register(httpConfig);
    
        app.UseWebApi(httpConfig);
        app.UseCors(CorsOptions.AllowAll);
    
        RouteConfig.RegisterRoutes(RouteTable.Routes);//MVC Routing
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    

    Is RouteConfig.RegisterRoutes(RouteTable.Routes) nough?

    Whenever I browse to any MVC route I get a 404.

  • Steven Yates
    Steven Yates almost 8 years
    Thanks for the clarification, the mvc routes are not being picked up. It's very strange. Can't see what I might have done incorrectly.