MVC 3.net 我想在 url 末尾添加一个 anchor 。
我试图包含一个 anchor 查询字符串,但哈希 '#' 在 url 中更改为 %23 或类似的东西。
有没有办法解决这个问题?
最佳答案
ActionLink 过载允许您指定片段的助手:
@Html.ActionLink(
"Link Text", // linkText
"Action", // actionName
"Controller", // controllerName
null, // protocol
null, // hostName
"fragment", // fragment
new { id = "123" }, // routeValues
null // htmlAttributes
)
将产生(假设默认路由):
<a href="/Controller/Action/123#fragment">Link Text</a>
更新:
如果您想在执行重定向的 Controller 操作中执行此操作,您可以使用 GenerateUrl方法:
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);
}
关于asp.net-mvc-3 - 如何将 anchor 标记添加到我的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7904835/