Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 } ); 
like image 772
Dean Avatar asked Aug 20 '12 18:08

Dean


People also ask

What is the difference between MVC routing and Web API routing?

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.

What is difference between conventional and attribute routing?

In short, Convention Routing approaches Routing from the general case; you generally add some routes that will match all or most of your URLs, then add more specific routes for more specialized cases. The other way to approach this problem is via Attribute Routing.

What is MVC endpoint routing?

In other words, endpoint routing decouples the route matching and endpoint dispatching functions, giving you the flexibility to combine different middleware (MVC, CORS, Razor Pages, Blazor, etc.) in your applications. To continue reading this article register now. Get Free Access.

Why we use routing in MVC?

Routing enables us to define a URL pattern that maps to the request handler. This request handler can be a file or class. In ASP.NET Webform application, request handler is . aspx file, and in MVC, it is the Controller class and Action method.


1 Answers

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

like image 88
Filip W Avatar answered Sep 25 '22 20:09

Filip W