Search code examples
asp.net-coreurlrouteslocation

Custom routing in anchor tag helper with ASP.NET Core


In my controller I am using route:

[Route("/{culture}/contact")]
//action signature

So I need to use a culture parameter. When entering directly url: https://localhost:44361/en/contact in the browser it is displayed same url, and it is fine.

In cshtml I am using an anchor such as:

<a class="mainButton" rel="canonical" asp-controller="Home" asp-action="Contact" asp-route-culture ="@ViewBag.Culture">contact</a>

And the anchor produces me an url in the browser: https://localhost:44361/contact?culture=en

How to make my anchor to produce this? https://localhost:44361/en/contact


Solution

  • At the end of the day I used approach where I am sending from the controller an ViewBag with culture code, and constructing the url in the view as follows:

    <div><a class="mainButton" rel="canonical" hreflang="@ViewBag.Culture" href="/@ViewBag.Culture/services">@Localizer["services"]</a></div>