将QueryString附加到asp.net核心锚点帮助器标签中的href [英] Append QueryString to href in asp.net core Anchor Helper Tag

查看:110
本文介绍了将QueryString附加到asp.net核心锚点帮助器标签中的href的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在请求的查询中添加任何内容以将锚定到html结果中:

I am trying to add anything in the query of the request to anchors in the html result:

虚构示例:

用户发出请求(请注意,乐队和歌曲可以是任何东西,我有一条路线可以满足此请求:template: {band} / {song}):

User makes a request (note that band and song could be anything, I have a route catering this request: template: "{band}/{song}"):

http://mydomain/band/song?Param1=111&Param2=222

现在,我希望锚点将查询字符串部分附加到锚点的href中。因此,我尝试了以下操作(请注意 asp-all-route-data):

Now I want my anchors to append the query string part to the href of my anchors. So I tried something like this (note the 'asp-all-route-data'):

<a asp-controller="topic" asp-action="topic" asp-route-band="iron-maiden" asp-route-song="run-to-the-hills" asp-all-route-data="@Context.Request.Query.ToDictionary(d=>d.Key,d=>d.Value.ToString())">Iron Maiden - Run to the hills</a>

查询字符串的追加实际上与上面的代码一起使用,但随后是铁娘子,结果是走上坡路。上面的标签帮助程序返回以下内容(请注意该帮助程序如何将请求中的乐队和歌曲镜像到href中,而不是我在asp-route属性中指定的乐队和歌曲中):

The append of the query string actually works with the above code but then the "iron-maiden" and "run-to-the-hills" are lost in the result. The above tag helper returns the following (note how the helper mirrors the band and song in the request into the href and not the band and song I specified in the asp-route attributes):

<a href="http://mydomain/band/song?Param1=111&Param2=2222">Iron Maiden - Run to the hills</a>

我希望帮助器产生以下结果:

I expect the following result from the helper:

<a href="http://mydomain/iron-maiden/run-to-the-hills?Param1=111&Param2=2222">Iron Maiden - Run to the hills</a>

似乎当我使用 asp-all-route-data 我在结果中松了 asp-route-band asp-route-song 值。

It seems like when I use the asp-all-route-data I loose the asp-route-band and asp-route-song values in the result.

有人

谢谢

Hooroo

推荐答案

似乎还没有官方的方法。

There doesn't seem to be any official way to do this yet.

如果 @ Context.GetRouteData()。Values 可以使用它代替。其背后的想法是, GetRouteData 从路由中间件获取当前路由信息作为键值对(字典),其中还应包含查询参数。

If the @Context.GetRouteData().Values works you should use it instead. The idea behind it is, that GetRouteData gets the current route information from the routing middleware as key value pairs (Dictionary) which should also contain query parameters.

我不确定它是否适用于您的情况以及 asp-route-band &在您的情况下, asp-route-song 是硬编码的或从路由中提取的。

I am not sure if it works in your case and if asp-route-band & asp-route-song are hard-coded or taken from route in your case.

工作,您可以尝试以下扩展方法&类:

In case that may not work, you could try the following extension method & class:

public static class QueryParamsExtensions
{
    public static QueryParameters GetQueryParameters(this HttpContext context)
    {
        var dictionary = context.Request.Query.ToDictionary(d => d.Key, d => d.Value.ToString());
        return new QueryParameters(dictionary);
    }
}

public class QueryParameters : Dictionary<string, string>
{
    public QueryParameters() : base() { }
    public QueryParameters(int capacity) : base(capacity) { }
    public QueryParameters(IDictionary<string, string> dictionary) : base(dictionary) { }

    public QueryParameters WithRoute(string routeParam, string routeValue)
    {
        this[routeParam] = routeValue;

        return this;
    }
}

它基本上是从扩展方法的上方抽象出您的代码并返回 QueryParameters 类型(它是扩展的 Dictionary< string,string> )类型,并使用一个单独的纯方法方便,因此您可以链接多个 .WithRoute 调用,因为字典的 Add 方法具有 void 返回类型。

It basically abstracts your code from above behind a extension method and returns a QueryParameters type (which is an extended Dictionary<string,string>) with a single additional method for pure convenience, so you can chain multiple .WithRoute calls, since Add method of dictionary has a void return type.

您将从这样的视图中调用它

You'd be calling it from your View like this

<a  asp-controller="topic"
    asp-action="topic" 
    asp-all-route-data="@Context.GetQueryParameters().WithRoute("band", "iron-maiden").WithRoute("song", "run-to-the-hills");"
>
    Iron Maiden - Run to the hills
</a>

这篇关于将QueryString附加到asp.net核心锚点帮助器标签中的href的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆