Attribute Routing in ASP.NET Core

In this article, we will look at the Attribute Routing in ASP.NET Core Apps. We learnt the basics of Routing in the last tutorial. Routing is how ASP.NET Core matches an incoming URL to a controller action.

There are two ways in which you can configure routing in ASP.NET Core. 

  1. Convention based Routing
  2. Attribute-based Routing

We learnt Convention based Routing in the last tutorial. We will look what is Attribute based routing in this article.

What is Attribute Routing

The attribute routing uses the attributes defined directly on the controller action to define the routes. Attribute routing gives you more control over the URLs in your web application.

In convention based routing all the routing is configured in the Startup class. In Attribute Routing routing is configured at the controller level.

How to Setup Attribute Routing

The Routes are added using the Route Attribute on the controller Action. The Route Attribute takes three arguments, URL Pattern, name and order.  

In the following code, we have added the route attribute on the index action method, passing “Home” as the URL Pattern.

In the above example, the /Home URL invokes the index method of the HomeController class. That is because The Route attribute expects the complete route to the action method.

 

How to Use Attribute Routing

Open the Project we created in the previous tutorial. 

Now, Open the startup.cs and locate the Configure method. Change the code as below

 

Here, we are using the app.UseMvc(), which registers the MVC Middleware, with no route.

Now, Open the HomeController.cs.

Add the Route(“Home”) attribute to the index action method

When we call the URL /Home the Index Action method of the HomeController is invoked.

URLMatch ?Parsed As
/No
/HomeYesController=Home
Action=Index
/Home/IndexNo

Now change the URL Pattern to “Home/Index”.

URLMatch ?Parsed As
/No
/HomeNo
/Home/IndexYes
Controller=Home
Action=Index

Changing the Name of the Action Method

The Very important to note that the URL Pattern does not need to match the Controller and Action Name. 

For Example

URLMatch ?Parsed As
/No
/HomeNo
/Home/IndexNo
/Say/HelloYesController=Home
Action=Index

 

Multiple Routes

You can also set multiple routes to a single action using Attribute Routing  as shown below

URLMatch ?Parsed As
/Yes
Controller=Home
Action=Index
/HomeYesController=Home
Action=Index
/Home/IndexYes
Controller=Home
Action=Index
/Say/HelloNo

Token Replacement

The Attribute Routing makes it easier for us by providing the tokens for [area], [controller], and [action].

These tokens get replaced by their actual values in the Routes collection       

The tokens in the URL Pattern ensures that the Controller and Action names stay sync with the name of the controller class.

Parameters to Action Methods

Just like in Convention based Routing, We can define additional URL Parameter placing them inside curly braces.  This can be passed as parameters to the action method.

In the above example, id is an Optional parameter. Any Value Received is injected as a parameter to Index Action method.

Combining Routes

We can specify the route attributes on the Controller class. Any URL pattern defined in the Controller class is prepended to the URL Pattern in the Action method.

This works fine for the Route Home/Index.  If the Pattern begins with a /  are considered to be absolute path and do not get combined with the parent URL Pattern 

This Following will not work with the URL Home/Index.     

 

Mixed Routing

You can use both convention-based routing and attribute routing in the same project. However,  if you define attribute routing on an action, then convention based routing cannot be used on that Action. 

Action Verbs

Attribute route can also be used with HTTP verb attributes like HttpGet, HttpPost etc.      

Summary

We looked at How to setup and work with attribute routing in ASP.NET Core Apps. We Register the URL Pattern in the Route Attribute on the controller Action methods. 

2 thoughts on “Attribute Routing in ASP.NET Core”

  1. Thanks – this was very helpful. What about multiple parameters – How would I create an endpoint to the request api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844 using attribute routing in the controller?

    1. TekTutorialsHub

      create a view model

      And in controller

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top