71

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 }
);
Martin Devillers
  • 17,293
  • 5
  • 46
  • 88
Dean
  • 1,281
  • 3
  • 15
  • 23
  • 7
    An interesting read that brings up that subject: [One (More) ASP.NET](http://www.paulstovell.com/one-more-aspnet). The blog post suggests that WebAPI's plumbing is very different because it is designed to be hosted outside of IIS, whereas the other routing mechanism was not. – vcsjones Aug 20 '12 at 18:18

2 Answers2

65

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

Filip W
  • 27,097
  • 6
  • 95
  • 82
29

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

Martin Devillers
  • 17,293
  • 5
  • 46
  • 88