• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Monday, August 31, 2020

ASP.NET Core Attribute Routing using Tokens

 Admin     August 31, 2020     .Net, .Net Core, .Net Core MVC, Asp.Net, C#     No comments   

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 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.

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.

Summary:
I hope this post will be helpful to understand the concept of ASP.NET Core Attribute Routing using Tokens
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Creating and Working with Database
    Hi friends! In our last post we have seen different approaches how we can connect with SQL Server Management Studio(SSMS). Today, We are g...
  • Entity Types in Entity Framework
    In this article, I am going to discuss the Entity Types in Entity Framework in detail. Please read our previous article where we discussed...
  • Introduction to Entity Framework
    Before .NET 3.5 as a developer, we often used to write ADO.NET code to perform CRUD operation with the underlying database. For this, we ne...
  • Filters in ASP.Net MVC
    Hi friends! Today we are going to discuss about using a very important feature of MVC i.e. “Filters“ . Filters are a unique feature of Asp...
  • Data Parallelism in C#
    Introduction: Hi, in this blog we are going to discuss a very important feature of C# that is data parallelism. Data parallelism means the ...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape