Mvc: AnchorTagHelper Append Routes To Existing Url

Created on 18 Dec 2015  路  4Comments  路  Source: aspnet/Mvc

I would like to use the anchor tag helper to "append" routes onto an existing URL of the page. For example, I am on a page with URL /search?q=foo.

I would like to create an anchor on that page that then adds &z=bar to the URL.

I tried the following:

<a asp-route-z="bar">Load</a>

But that generates /search?z=bar.

I want it to generate /search?q=foo&z=bar.

I know I could do this by something like:

<a asp-route-q="@query["q"]" asp-route-z="bar">Load</a>

but when my page has a large number of possible query strings I do not want to have to type them out all the time. I just want to append, or overwrite, new query string routes to the existing page URL.

Is this possible with existing tag helpers or will it require something custom?

enhancement wontfix

Most helpful comment

@kspearrin I've been using this as a workaround. You can pull it into an extension method to make it easier to use.

<a asp-all-route-data="Context.Request.Query.ToDictionary(q => q.Key, q => q.Value.ToString())" asp-route-z="bar" />

All 4 comments

Sorry @kspearrin it's currently not possible with TagHelpers to do what you're asking. However, the workarounds you mentioned are appropriate alternatives.

Routing in general offers no such thing, but this could be a cool feature in the future.

@kspearrin I've been using this as a workaround. You can pull it into an extension method to make it easier to use.

<a asp-all-route-data="Context.Request.Query.ToDictionary(q => q.Key, q => q.Value.ToString())" asp-route-z="bar" />

No plans to implement this feature.

Was this page helpful?
0 / 5 - 0 ratings