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?
Sorry @kspearrin it's currently not possible with TagHelper
s 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.
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" />