MapRoute or MapHttpRoute in MVC WebAPI

Since WebApi route mapping can be done with MapHttpRoute:

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

      

Do I also need to map routes using MapRoute like this:

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

      

  • First question: correct me if I am wrong. I believe MapRoute is meant to support MVC pages like help page etc ... so if I don’t have any MVC page for my Api do I still need to implement the routes.MapRoute part?

  • Second question: if I need to add any mvc page to my Api page (like a man page) it can be done with MapHttpRoute

+3


source to share


1 answer


First question

If you are not using MVC you do not need MapRoute

Second question

Not. MapHttpRoute will only target action methods within classes derived from ApiController (or IHttpController implementation, but this is a tentative scenario in most cases where you will descend from ApiController).

On the other hand, browsing the namespace is quite intuitive.



MapRoute

is a method of the RouteCollectionExtensions class which is in the System.Web.Mvc namespace, see this for more information

and

MapHttpRoute

is a method of the HttpRouteCollectionExtensions class which is in the System.Web.Http namespace, see this for more information

Here System.Web.Http is WebAPI related.

+3


source







All Articles