ASP.NET Core Routing Attributes
In this article, we will cover ASP.NET Core Routing Attributes. So, Let’s get started.
ASP.NET Core Routing
Routing in ASP.NET Core is the process of mapping incoming requests to application logic that resides in controllers and methods. ASP.NET Core maps the incoming request based on the routes that you configure in your application, and for each route, you can set specific configurations, such as default values, message handlers, constraints, and so on.
Types of Routing
- Convention-Based Routing
- Attribute-Based Routing.
Convention-Based Routing
The route is determined based on conventions that are defined in route templates that, at runtime, will map requests to controllers and actions (methods).
Attribute-Based Routing
The route is determined based on attributes that you set on your controllers and methods. These will define the mapping to the controller’s actions.
HTTP methods to perform CRUD operations.
Example
HttpHead specifies that the action should be called for HEAD requests.
Note: Identical to GET except that server do not return the message body.
HttpOptions specifies that the action should be called for OPTIONS requests
HttpPatch specifies that the action should be called for PATCH requests
AcceptVerbs specifies that the action should be called for one or more HTTP verbs.
Route specifies a route template for the controller.
[HttpGet(“{Code}”)] specifies Get request with “code” route parameter.
[HttpGet(“{id:int}”)] specifies Get request with integer “id” route parameter.
[HttpGet(“{id:length(10)}”)] specifies Get request with 10-character long “id” route parameter.
[HttpGet(“{id: regex(^\d{{3}}-\d{{2}}-\d{{4}}$)}”)] specifies Get request with “id” route parameter matching a regex.
[HttpGet(“employee/{department:alpha:minlength (3)}”)] specifies Get request for a 3+ character alphabetic “department” parameter.