Attribute routing in ASP NET Web API 204:33

  • 0
Published on May 9, 2017

For the example code used in the demo, please refer to the text article of this video.

Slides

All ASP .NET Web API Text Articles and Slides

All ASP .NET Web API Videos

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

In this video we will discuss Attribute Routing introduced in ASP.NET Web API 2. Let us understand attribute routing with an example

What is Attribute Routing
Using the [Route] attribute to define routes is called Attribute Routing

What are the advantages of using Attribute Routing
Attribute routing gives us more control over the URIs than convention-based routing. Creating URI patterns like hierarchies of resources (For example, students have courses, Departments have employees) is very difficult with convention-based routing. With attribute routing all you have to do is use the [Route] attribute as shown below.

[Route(“api/students/{id}/courses”)]

How to enable Attribute Routing
In ASP.NET Web API 2, Attribute Routing is enabled by default. The following line of code in WebApiConfig.cs file enables Attribute Routing.
config.MapHttpAttributeRoutes();

Can we use both Attribute Routing and Convention-based routing in a single Web API project
Yes, both the routing mechanisms can be combined in a single Web API project. The controller action methods that have the [Route] attribute uses Attribute Routing, and the others without [Route] attribute uses Convention-based routing.

https://cafeadobro.ro/

https://www.stagebox.uk/wp-includes/depo10-bonus10/

https://iavec.com.br/

Enjoyed this video?
"No Thanks. Please Close This Box!"