Question

In my MVC and WebAPI application I see two different ways of doing routes.

  • One for MVC which calls RegisterRoutes and passes RouteTable.Routes
  • One for Web API which calls CustomizeConfig and passes GlobalConfiguration.Configuration.

For WebAPI:

WebApiConfig.CustomizeConfig(GlobalConfiguration.Configuration);

public static void Register(System.Web.Http.HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: ApiControllerOnly,
        routeTemplate: "api/{controller}");
}

For MVC:

RouteConfig.RegisterRoutes(RouteTable.Routes);

public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
{
    routes.MapRoute("DefaultRedirect",
        "",
        new { controller = "Home", action = "Index" });
}

Can someone explain if there is any difference in me registering my routes in one or the other method calls? Also why is it done this way with one using.

Was it helpful?

Solution

MVC routes register with ASP.NET (system.web) route collection. Web API however is designed to run either in IIS on top of system.web or as a self host without changing the code.

Hence Web API has a different registration mechanism, where it can use the system.web routing under the hood, or it's own routing system when using self hosting (Either WCF self host, or Owin host are supported out of the box).

There is one other small difference, Web API routes require naming of the route, where MVC routes do not.

OTHER TIPS

One of the significant differences in the web API compared to traditional ASP.NET MVC controllers is how the web API will route request into the action methods.

With the web API, the HTTP method being used plays a role. The HTTP method is the verb used in the HTTP message and the common verbs are get, post, put, and delete. Those are the verbs that the web API will route by default. You an also handle additional verbs if you need to do something like webDAV. You can do that and handle additional verbs by using an except verbs attribute.

What the web API will do is if there is a request for "/movies", the web API will look for a movie controller and then look for a method on that controller starting with the word "Get." So I could have an action called get movies and because this is an HTTP get message, the framework will invoke get movies. But you could also call it GET whatever is just because it starts with the letters G-E-T that's why that particular action method will receive the request.

The web API also registers these routes slightly differently since there is no action that's going to be in the URL for the routing engine to pick apart, it's using the verb instead. If you look at the default route configuration for a web API, it's done with an extension method MapHttpRoute.

For Web API it is:

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

For MVC, it is:

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

Notice that for Web API there is no action in the URL template and it's also important to note that the path of this needs to start with "api".

After looking at this, the proper way to reach a controller (say Movies) is to use /api/movies as a URL on the browser. That will invoke the get method since we have a get request.

Note: Web API controllers inherit from System.Web.Http.Controller, but MVC controllers inherit from System.Web.Mvc.Controller. Both the libraries are different but acts in similar fashion. Web API's MapHttpRoute is defined as an extension method in System.Web.Http class. MVC's MapRoute is defined in System.Web.Mvc as an extension method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top