使用 Asp.Net MVC ActionLink、RedirectToAction 等时有没有办法包含片段标识符?
-
08-06-2019 - |
题
我想要一些链接包含片段标识符。就像这个网站上的一些网址一样:
调试:IE6 + SSL + AJAX + post表单 = 404错误#5626
有没有办法使用 MVC 中的任何内置方法来做到这一点?或者我必须推出自己的 HTML 助手吗?
解决方案
我们正在考虑在下一个版本中加入对此的支持。
其他提示
正如 Brad Wilson 所写,您可以通过简单地连接字符串来在视图中构建自己的链接。但是要将片段名称附加到通过 RedirectToAction (或类似的)生成的重定向,您将需要如下所示的内容:
public class RedirectToRouteResultEx : RedirectToRouteResult {
public RedirectToRouteResultEx(RouteValueDictionary values)
: base(values) {
}
public RedirectToRouteResultEx(string routeName, RouteValueDictionary values)
: base(routeName, values) {
}
public override void ExecuteResult(ControllerContext context) {
var destination = new StringBuilder();
var helper = new UrlHelper(context.RequestContext);
destination.Append(helper.RouteUrl(RouteName, RouteValues));
//Add href fragment if set
if (!string.IsNullOrEmpty(Fragment)) {
destination.AppendFormat("#{0}", Fragment);
}
context.HttpContext.Response.Redirect(destination.ToString(), false);
}
public string Fragment { get; set; }
}
public static class RedirectToRouteResultExtensions {
public static RedirectToRouteResultEx AddFragment(this RedirectToRouteResult result, string fragment) {
return new RedirectToRouteResultEx(result.RouteName, result.RouteValues) {
Fragment = fragment
};
}
}
然后,在你的控制器中,你可以调用:
return RedirectToAction("MyAction", "MyController")
.AddFragment("fragment-name");
这应该会正确生成 URL。
在MVC3中(可能之前我还没有检查过),您可以使用UrlHelper.GenerateUrl传入fragment参数。这是我用来包装功能的辅助方法
public static string Action(this UrlHelper url, string actionName, string controllerName, string fragment, object routeValues)
{
return UrlHelper.GenerateUrl(
routeName: null,
actionName: actionName,
controllerName: controllerName,
routeValues: new System.Web.Routing.RouteValueDictionary(routeValues),
fragment: fragment,
protocol: null,
hostName: null,
routeCollection: url.RouteCollection,
requestContext: url.RequestContext,
includeImplicitMvcValues: true /*helps fill in the nulls above*/
);
}
@多米尼克,
我几乎肯定将其放入路线中会导致路线问题。
@瑞奇,
在 MVC 对此提供支持之前,您可以在如何制作路线方面采取更“老派”的方式。例如,您可以转换:
<%= Html.ActionLink("Home", "Index") %>
进入:
<a href='<%= Url.Action("Index") %>#2345'>Home</a>
或者您可以编写自己的助手来完成基本相同的事情。
简短的回答是:不。在 ASP.NET MVC Preview 3 中,没有一流的方法可以在操作链接中包含锚点。与 Rails 的 url_for :anchor 不同,UrlHelper.GenerateUrl(以及使用它的 ActionLink、RedirectToAction 等)没有可以让您对锚点进行编码的神奇属性名称。
正如您所指出的,您可以自己推出。这可能是最干净的解决方案。
简单地说,您可以在路由中包含一个锚点并在参数哈希中指定值:
routes.MapRoute("WithTarget", "{controller}/{action}/{id}#{target}");
...
<%= Html.ActionLink("Home", "Index", new { target = "foo" })%>
这将生成一个类似 /Home/Index/#foo 的 URL。不幸的是,这对于出现在 URL 末尾的 URL 参数来说效果不佳。因此,此 hack 仅在所有参数都显示为 URL 路径组件的非常简单的情况下才有效。
这是一个客户端解决方案,但如果您有可用的 jquery,您可以执行类似的操作。
<script language="javascript" type="text/javascript">
$(function () {
$('div.imageHolder > a').each(function () {
$(this).attr('href', $(this).attr('href') + '#tab-works');
});
});
</script>
MVC 5 支持片段标识符。看 ActionLink
的过载 https://msdn.microsoft.com/en-us/library/dd460522(v=vs.118).aspx 和 https://msdn.microsoft.com/en-us/library/dd492938(v=vs.118).aspx.