代码之家  ›  专栏  ›  技术社区  ›  Peter Mounce

如何将锚点/哈希的参数添加到RedirectToRouteResult?

  •  10
  • Peter Mounce  · 技术社区  · 17 年前

    5 回复  |  直到 14 年前
        1
  •  14
  •   dariol    16 年前

    您应该在路由表中正确构建路由。如。:

    routes.MapRoute("MyRoute" ,"{controler}/{id}#{detail}" ,new { controller = "users", action = "index", id = (string)null, detail = "Summary" });
    
        2
  •  6
  •   Joel Dart    14 年前

            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*/
                );
            }
    

    然后我创建了一个RedirectToFragmentResult类

    public class RedirectToFragmentResult: RedirectResult
    {
        public UrlHelper Url {get;set;}
        public string Action { get; set; }
        public string Controller { get; set; }
        public string Fragment { get; set; }
        public object RouteValues { get; set; }
        public RedirectToFragmentResult(UrlHelper url, string action, string controller, string fragment, object routeValues)
            :base(url.Action(action, controller, fragment, routeValues))
        {
            Url = url;
            Action = action;
            Controller = controller;
            Fragment = fragment;
            RouteValues = routeValues;
        }
    }
    

    然后,您可以在单元测试中创建一个新的RouteValueDictionary(result.RouteValues),以使用RedirectToRouteResult检查它。

        3
  •  1
  •   Community Mohan Dere    9 年前

    我也有类似的问题,请看这里:

    Linking to location on page (#id) through ASP.NET MVC mechanisms?

    最后我用#创建了一条路线。

        4
  •  0
  •   Matt Kocaj    17 年前

    我在我的网站上做了一些类似的事情 here RedirectToRouteResult . 重定向路由结果 不支持在url中包含锚定部分。

    您需要自己构建链接,甚至是处理锚定部分处理的逻辑——就像我所做的那样。我的应用程序尝试复制类似于Facebook照片库视图的功能。每个指向不同页面的链接都必须有一个唯一的url,因此我使用锚定部分。但是因为它不能直接转换为路由,我必须在页面上手动解析url的锚定部分,并使用ajax加载适当的内容。这是我想要的,所以对我来说很有用。

        5
  •  0
  •   TFD    16 年前

    下载 source code of MVC

    除了对RedirectToRouteResult的简单继承和对 允许可选锚定部件的ExecuteSult应该可以解决此问题