Difference between "MapHttpRoute" and "MapRoute"?

C#asp.netasp.net Mvcasp.net Web-Apiasp.net Mvc-Routing

C# Problem Overview


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 }
);

C# Solutions


Solution 1 - C#

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).

Solution 2 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDeanView Question on Stackoverflow
Solution 1 - C#Filip WView Answer on Stackoverflow
Solution 2 - C#Martin DevillersView Answer on Stackoverflow