DEV Community

Cover image for Anchor Tag Helper in ASP.NET Core 2.1
Hòa Nguyễn Coder
Hòa Nguyễn Coder

Posted on

Anchor Tag Helper in ASP.NET Core 2.1

In the Article previous, I say to TagHelper in ASP.NET Core 2.1, in this article I share more information about this library.
You can see again the article previous: Create Project ASP.NET Core 2.1
The Tag Helper is a library help insert URL in element , In ASP.NET MVC 5 you can call URL @Url.Action(). But in ASP.NET Core 2.1 we use simple more much
You can see it : https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-2.1

<ul class="nav navbar-nav">
                    <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
                    <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
                    <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
                    <li><a asp-area="" asp-controller="Home" asp-action="Detail" asp-route-id="2">Detail</a></li>
                    <li><a asp-area="" asp-controller="Home" asp-action="Product" asp-all-route-data="parms">Product</a></li>
                    <li><a asp-route="ProductCategory" asp-all-route-data="parms">Product2</a></li>
  </ul>
Enter fullscreen mode Exit fullscreen mode

Example: ProductID=2, CategoryID=4

  //HomeController.cs
  [Route("/Home/Product",
         Name = "ProductCategory")]
  public IActionResult Product(int ProductId,int CategoryId)
  {
       return View();
  }
Enter fullscreen mode Exit fullscreen mode

You can use it the following code below

  @{
  var parms = new Dictionary<string, string>
              {
                  { "ProductID", "11" },
                  { "CategoryID", "4" }
              };
  }
  <a asp-controller="Home" asp-action="Product" asp-all-route-data="parms">Product</a>
  //or
  <a asp-route="ProductCategory" asp-all-route-data="parms">Product</a>
Enter fullscreen mode Exit fullscreen mode

Through this article you can imagine , we use Tag Helper call URL, insert params to URL
The Article : Anchor Tag Helper in ASP.NET Core 2.1

Top comments (0)