【发布时间】:2017-08-30 23:31:54
【问题描述】:
我正在尝试将请求查询中的任何内容添加到 html 结果中的锚点:
虚构的例子:
用户提出请求(请注意,乐队和歌曲可以是任何东西,我有一条满足此请求的路线:模板:“{band}/{song}”):
http://mydomain/band/song?Param1=111&Param2=222
现在我希望我的锚将查询字符串部分附加到我的锚的 href 中。所以我尝试了这样的事情(注意'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>
查询字符串的追加实际上适用于上述代码,但结果中丢失了“iron-maiden”和“run-to-the-hills”。上面的标签助手返回以下内容(注意助手如何将请求中的乐队和歌曲镜像到 href 中,而不是我在 asp-route 属性中指定的乐队和歌曲):
<a href="http://mydomain/band/song?Param1=111&Param2=2222">Iron Maiden - Run to the hills</a>
我希望助手会得到以下结果:
<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 结果中的值。
有没有人偶然发现过这个?
谢谢
呵呵
【问题讨论】:
-
似乎没有任何官方方式,但我可能有一个解决方法的想法,但首先:
@Context.GetRouteData().Values工作吗? GetRouteData 从路由中间件获取路由作为键值对,还应该包含查询参数。不确定它是否适用于您的情况,以及 asp-route-band 和 asp-route-song 是硬编码还是从您的情况下取自路由
标签: asp.net asp.net-mvc asp.net-core tag-helpers asp.net-core-tag-helpers