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
source to share
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.
source to share