Difference between "MapHttpRoute" and "MapRoute"?

Why using "MapRoute" for "Default" routing, while using "MapHttpRoute" for "DefaultApi" routing?

routes.MapHttpRoute(
  name: "DefaultApi",
  routeTemplate: "api/{controller}/{action}"
);

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

If you use Web API on top of ASP.NET they would ultimately both operate on the same underlying ASP.NET route table - however as correctly pointed out, from the user perspective you call two different methods to register route.

Routing was designed like this so that when hosting outside of ASP.NET, Web API wouldn't have to rely on System.Web.

Bear in mind that Web API is not sitting on top of MVC, Web Forms, or, for that matter ASP.NET at all. It can be hosted within web context (ASP.NET) but can also be self hosted (Console, WPF etc) or even hosted in memory (without port use, useful for i.e. lightweight end-to-end testing).


MapRoute is meant for "normal" ASP.NET MVC controllers whereas MapHttpRoute is meant for Web API controllers.