Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5, Web API And Owin

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.

like image 763
Steven Yates Avatar asked May 13 '16 20:05

Steven Yates


People also ask

What is the use of OWIN in Web API?

Open Web Interface for . NET (OWIN) defines an abstraction between . NET web servers and web applications. OWIN decouples the web application from the server, which makes OWIN ideal for self-hosting a web application in your own process, outside of IIS.

Is OWIN supported in .NET core?

ASP.NET Core: Supports the Open Web Interface for . NET (OWIN).

What is OWIN MVC?

Owin is the under the hood interface between web servers and web applications. If you only write web applications in a single framework (such as ASP.NET MVC) an only run on one server platform (Windows with IIS) you can ignore Owin.


2 Answers

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.

like image 134
robrich Avatar answered Oct 05 '22 13:10

robrich


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.

like image 42
vendettamit Avatar answered Oct 05 '22 12:10

vendettamit