如何将锚标记添加到我的 URL? [英] How can I add an anchor tag to my URL?
本文介绍了如何将锚标记添加到我的 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MVC 3.net 我想在 url 末尾添加一个锚点.
MVC 3.net I want to add an anchor to the end a url.
我试图在 URL 中包含一个锚查询字符串,但哈希 '#' 更改为 %23 或类似的内容.
I tried to include an anchor query string but the hash '#' changes to %23 or something like that in the url.
有没有办法解决这个问题?
Is there a way of working around this?
推荐答案
ActionLink 过载 允许您指定片段的帮助器:
There is an overload of the ActionLink helper that allows you to specify the fragment:
@Html.ActionLink(
"Link Text", // linkText
"Action", // actionName
"Controller", // controllerName
null, // protocol
null, // hostName
"fragment", // fragment
new { id = "123" }, // routeValues
null // htmlAttributes
)
将产生(假设默认路由):
will produce (assuming default routes):
<a href="/Controller/Action/123#fragment">Link Text</a>
<小时>
更新:
如果您想在执行重定向的控制器操作中执行此操作,您可以使用 GenerateUrl 方法:
and if you wanted to do this within a controller action performing a redirect you could use the GenerateUrl method:
public ActionResult Index()
{
var url = UrlHelper.GenerateUrl(
null,
"Action",
"Controller",
null,
null,
"fragment",
new RouteValueDictionary(new { id = "123" }),
Url.RouteCollection,
Url.RequestContext,
false
);
return Redirect(url);
}
这篇关于如何将锚标记添加到我的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文