Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between two attribute-based routing approaches: HttpGet("") and Route("")?

I was looking for information about attribute-based routing and found that there are two different attributes one can use: HttpGet("") and Route(""). However, I can't find any information about what the difference is between them.

Does one of them exist in order to support old ASP versions, or this there a different reason?

P.S. My code might not be totally correct, because I have just started to learn ASP. If something is not clear, I will try to explain.

public class MyController : Controller {     // APPROACH 1     [Route("api/books")]     [HttpGet]     public async List<Book> GetBooks()      {         // Implementation     }      // APPROACH 2     [HttpGet("api/books")]     public async List<Book> GetBooks()     {         // Implementation     } } 
like image 345
Sviatoslav V. Avatar asked Jul 10 '18 11:07

Sviatoslav V.


People also ask

What is difference between route and Routeprefix?

So as long as you are using attribute routing you are going to be using the [RouteAttribute] . Without this attribute the action will default back to convention-based routing. The RoutePrefixAttribute is an extensibility point that allows you more control of how you define your routes/Urls.

What is difference between attribute and conventional routing?

As per our opinion, Attribute Routing provides us more flexibilities as compared to Convention Based Routing. In Attribute Routing, we can manage route as controller level, action level and also area level. Also, it can override the child route, if required.

What is routing and attribute routing?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

What is attribute-based routing?

Attribute-Based Routing is a process used by contact center applications to distribute tasks according to a set of defined characteristics.


1 Answers

Route is method unspecific, whereas HttpGet obviously implies that only GET requests will be accepted. Generally, you want to use the specific attributes: HttpGet, HttpPost, etc. Route should be used mostly on controllers to specify the base path for all actions in that controller. The one exception is if you're creating routes for exception handling / status code pages. Then, you should use Route on those actions, since requests via multiple methods could potentially be routed there.

like image 120
Chris Pratt Avatar answered Sep 21 '22 04:09

Chris Pratt