ASP.NET Core Attribute Routing using Tokens

ASP.NET Core Attribute Routing using Tokens

In this article, I am going to discuss the ASP.NET Core Attribute Routing using Tokens with examples. Please read our previous article before proceeding to this article as we are going to work with the same example that we worked in our previous article. In our previous article, we discussed Attribute Routing in ASP.NET Core MVC Application. As part of this article, we are going to discuss the following pointers.

  1. Understanding Tokens in Attribute Routing.
  2. Token Example in Attribute Routing.
  3. Advantages of using Tokens in Attribute Routing.
  4. Do we need to write the action token on each action methods?
  5. Attribute Routing vs Conventional Routing in ASP.NET Core.
Tokens in Attribute Routing:

In ASP.NET Core, the Route Attribute support token replacement. It means we can enclose the token (i.e. controller and action) within a pair of square-braces ([ ]). The tokens (i.e. [controller] and [action]) are then replaced with the values of controller and action method name where the route is defined. 

Token Example in Attribute Routing:

Let us understand this with an example. Please modify the Home Controller class as shown below. Here we are applying the token [controller] on the Home Controller and at the same time, we are applying the token [action] on all the action methods.

using Microsoft.AspNetCore.Mvc;
namespace FirstCoreMVCApplication.Controllers
{
    [Route("[controller]")]
    public class HomeController : Controller
    {
        [Route("[action]")]
        public string Index()
        {
            return "Index() Action Method of HomeController";
        }

        [Route("[action]")]
        public string Details()
        {
            return "Details() Action Method of HomeController";
        }
    }
}

With the above controller and action tokens in place, now you can access the Index action method of Home Controller with the URL /Home/Index. Similarly, you can access the Details action method using the URL /Home/Details. Now run the application and see everything is working as expected.

Advantages of Tokens in Attribute Routing:

The main advantage is that in future if you rename the controller name or the action method name then you do not have to change the route templates. The application is going to works with the new controller and action method names.

How to make the Index action method as the default action?

With the controller and action tokens in place, if you want to make the Index action method of Home Controller as the default action, then you need to include the Route(“”) attribute with an empty string on the Index action method as shown below. 

ASP.NET Core Attribute Routing using Tokens

Do we need to write the action token on each action methods?

Not Really. If you want all your action method to apply action token, then instead of including the [action] token on each and every action methods, you can apply it only once on the controller as shown below. 

using Microsoft.AspNetCore.Mvc;
namespace FirstCoreMVCApplication.Controllers
{
    [Route("[controller]/[action]")]
    public class HomeController : Controller
    {
        public string Index()
        {
            return "Index() Action Method of HomeController";
        }
        
        public string Details()
        {
            return "Details() Action Method of HomeController";
        }
    }
}
Attribute Routing vs Conventional Routing in ASP.NET Core:

In Attribute Routing, we need to define the routes using the Route attribute within the controller and action methods. The Attribute routing offers a bit more flexibility than conventional based routing. However, in general, the conventional based routings are useful for controllers that serve HTML pages. On the other hand, the attribute routings are useful for controllers that serve RESTful APIs.

However, there is nothing stopping you from mixing conventional based routing with attribute routing in a single application.

In the next article, I am going to discuss Layout View in ASP.NET Core MVC Application with one example. Here, in this article, I try to explain the need and use of ASP.NET Core Attribute Routing using Tokens. I hope you enjoy this article. 

Leave a Reply

Your email address will not be published. Required fields are marked *