Sunday, January 27, 2019

How do I use ASP.NET Core anchor tag helpers with non-default routes?

Hi /r/dotnet!

I'm trying use standard ASP.NET Core MVC routing to serve this template:

routes.MapRoute(

name: "project",

template: "{projectID}/{controller=Home}/{action=Index}/{id?}"

);

This works fine, the URL like /1/accounts/index passes projectID parameter to the Index action of Accounts controller.

However, I couldn't figure out how to generate links to this route with standard Tag Helpers. The official ASP.NET Core documentation doesn't seem cover this use case.

I tried

<a asp-route="project" asp-area="" asp-controller="Accounts" asp-action="Index" asp-route-projectID="1">Accounts</a>

but this results in error.

For now the workaround is to use

@Url.RouteUrl("project", new { controller = "Accounts", action = "Index", projectID = 1 })

but if I could use tag helpers it would be much nicer.

How do I use ASP.NET Core anchor tag helpers with non-default routes? Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team