Introduction
The Anchor Tag Helper generates an HTML anchor (<a> </a>) element by adding new attribute. The "href" attribute of the anchor tag is created by using the new attributes.
Anchor Tag Helper Attributes
The Anchor Tag Helper has introduced many attributes that help us to generate the HTML for Anchor Tag Helper. In this section, we will learn each attribute that can be used with Anchor Tag Helper.
The Anchor Tag Helper has introduced many attributes that help us to generate the HTML for Anchor Tag Helper. In this section, we will learn each attribute that can be used with Anchor Tag Helper.
asp-controller
It is used to associate the Controller name that is used to generate the URL. The Controller that is specified here must exist in the current project.
It is used to associate the Controller name that is used to generate the URL. The Controller that is specified here must exist in the current project.
asp-action
It is used to specify the name of the action method (in the Controller) that helps us to generate the URL. The generated URL is placed in the href attribute of the anchore tag.
It is used to specify the name of the action method (in the Controller) that helps us to generate the URL. The generated URL is placed in the href attribute of the anchore tag.
Example
In this following example, the Controller and the action both are set as default so it will generate the URL differently.
In this following example, the Controller and the action both are set as default so it will generate the URL differently.
- <a asp-controller="Home" asp-action="Index">All Users</a>
Generated HTML
- <a href="/">All Users</a>
- <a asp-controller="Home1" asp-action="Index1">All Users</a>
- <a href="/Home1/Index1">Users</a>
If the asp-action attribute is not specified with asp-controller attribute, it will use the default action method of the Controller to generate the URL. If asp-controller attribute is not specified with asp-action attribute, it will use the current View Controller by default. If the asp-action attribute is defined as "index", no action is specified to the URL and the default index method is called.
asp-route-{value}
This is a wild card route prefix. Any value that is placed after the trailing dash (in place of {value}) will be consider as a route parameter. If a default route is not found, this value will be used to generate the URL as a request parameter and value.
Example
This is a wild card route prefix. Any value that is placed after the trailing dash (in place of {value}) will be consider as a route parameter. If a default route is not found, this value will be used to generate the URL as a request parameter and value.
Example
- <a asp-controller="Home1" asp-action="Detail" asp-route-id ="12">User Data</a>
Generated HTML
- <a href="/Home1/Detail/12">User Data</a>
If route prefix is not a part of the route template, it will pass the route parameter as query string.
Example

Manoj Kumar SharmaPosted Jul 17, 2019, 11:40 AM
Tip: Adopt the approach offerred by the Microsoft.AspNetCore.Mvc.TagHelpers for route parameters. Instead of using the asp-all-route-data, where the parameters have to be defined as a dictionary and initialized before hand, before using the same as part of the <a> anchor tag, it is more effective to adopt the asp-route-<paramname> = <paramvalue> approach. For example, for the above shown CSHTML code, we could write it as: <a asp-route="speakerevalscurrent" asp-route-speakerId="11" asp-route-currentYear="true">Speaker Evaluations</a> Advantage: In a Listing page, for example, we would be iterating using the "item" member of the Model in the foreach loop. Accessing the ID parameter, for example of the "item" would become cumbersome, as the initialization of the dictionary would have to be achieved inside the foreach loop to adopt the approach as shown in the asp-all-route-data example above. Adopting the refined approach would look like: <a asp-action="Details" asp-controller="Manage" asp-area="Admin" asp-route-id="@item.ID" asp-route-param2="@item.CategoryName"> <span class="fa fa-eye" aria-hidden="true"></span> </a> The preceding code will generate the following HTML: <a href="/Admin/Manage/Details?ID=15¶m2=DemoCategory"> View Details </a>