Loading

ASP.NET Core

What is ASP.NET Core Attribute Routing using Tokens?. The Complete ASP.NET Core Developer Course 2023 [Videos].

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.

ASP.NET Core Attribute Routing using Tokens

In this Video, I am going to discuss the ASP.NET Core Attribute Routing using Tokens with examples. Please read our previous Video before proceeding to this Video as we are going to work with the same example that we worked in our previous Video. In our previous Video, we discussed Application. As part of this Video, 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 method?
  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 the 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 method?

Not Really. If you want all your action methods to apply action token, then instead of including the [action] token on each and every action method, 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.

See All

Comments (248 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /248

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /248

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /248