1.

When to use Attribute Routing?

Answer»

By using attribute routing, you can easily define some of the URI patterns that are most COMMON in RESTful APIs. For example, resources will usually have child resources such as movies have actors, Clients will have orders, books have authors, etc. So it’s obvious to create URIs that reflect these kinds of relations such as /clients/1/orders. It is difficult to create these kinds of URIs using convention-based routing. With the help of attribute routing, it becomes easier for defining a route to this URI. 

You need to ADD an attribute to the controller action as GIVEN below:

[Route("clients/{cId}/orders")]public IEnumerable<Order> ObtainOrdersByClient(int cId){ //Statements}

To enable attribute routing in ASP.NET MVC 5 application, you need to add a call to the method routes.MapMvcAttributeRoutes() WITHIN the RegisterRoutes() method of RouteConfig.cs file.



Discussion

No Comment Found