带锚的ASP.Net MVC RedirectToAction


84

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

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


2
对于ASP.NET MVC Core,请参阅stackoverflow.com/q/55741977/11683
GSerg

Answers:


153

我认为您应该同时使用该Redirect方法Url.RouteUrl来完成它。

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

像这个答案一样,但这是否会导致向客户端渲染,然后向服务器发出新请求,然后再进行另一个渲染?如果是这样,还有另一种方法可以避免这种情况?
雅克

1
此解决方案很难测试,但很可能是唯一的解决方案。
乔什·科德罗夫

19

另一种选择是Url.Action

return Redirect(Url.Action("ShowThreadLastPostPage", "Thread", new { threadOid = threadOid }) + "last#" + postOid);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.