【问题标题】:ASP.Net MVC RedirectToAction with anchor带有锚点的 ASP.Net MVC RedirectToAction
【发布时间】:2010-10-10 19:23:47
【问题描述】:

我有以下问题: 例如我有这样的路线:

routes.Add(new Route("forums/thread/{threadOid}/last", new MvcRouteHandler())
           Defaults = new RouteValueDictionary(
             new { controller = "Thread", action ="ShowThreadLastPostPage"}),
        Constraints = new RouteValueDictionary(new { threadOid = @"^\d+$" })
    }
);

有没有办法使用 RedirectToAction 方法导航到这样的 URL:

forums/thread/{threadOid}/last#postOid

【问题讨论】:

标签: asp.net asp.net-mvc fragment-identifier


【解决方案1】:

Url.Action 的另一种选择:

return Redirect(Url.Action("ShowThreadLastPostPage", "Thread", new { threadOid = threadOid }) + "last#" + postOid);

【讨论】:

    【解决方案2】:

    我认为你应该使用Redirect 方法和Url.RouteUrl 来完成它。

    return Redirect(Url.RouteUrl(new { controller = "Thread", action = "ShowThreadLastPostPage", threadOid = threadId }) + "#" + postOid);
    

    【讨论】:

    • 喜欢这个答案,但这不会导致向客户端渲染然后向服务器发出新请求,然后再进行另一个渲染吗?如果是这样,还有其他方法可以避免这种情况吗?
    • 这个解决方案很难测试,但它可能是唯一的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    相关资源
    最近更新 更多