ASP.Net MVC重定向到带有锚的操作
我有以下问题: 例如,我有这样的路线:ASP.Net MVC重定向到带有锚的操作,asp.net,asp.net-mvc,fragment-identifier,Asp.net,Asp.net Mvc,Fragment Identifier,我有以下问题: 例如,我有这样的路线: routes.Add(new Route("forums/thread/{threadOid}/last", new MvcRouteHandler()) Defaults = new RouteValueDictionary( new { controller = "Thread", action ="ShowThreadLastPostPage"}), Constraints = new
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的方法,如下所示:
routes.Add(new Route("forums/thread/{threadOid}/last", new MvcRouteHandler())
Defaults = new RouteValueDictionary(
new { controller = "Thread", action ="ShowThreadLastPostPage"}),
Constraints = new RouteValueDictionary(new { threadOid = @"^\d+$" })
}
);
forums/thread/{threadOid}/last#postOid
我认为您应该使用
Redirect
方法以及Url.RouteUrl
来完成它
return Redirect(Url.RouteUrl(new { controller = "Thread", action = "ShowThreadLastPostPage", threadOid = threadId }) + "#" + postOid);
另一种选择是
Url.Action
:
return Redirect(Url.Action("ShowThreadLastPostPage", "Thread", new { threadOid = threadOid }) + "last#" + postOid);
与此答案类似,但这是否会导致对客户端进行渲染,然后向服务器发出新请求,然后再进行另一次渲染?如果是这样的话,还有其他方法可以避免这种情况吗?这个解决方案很难测试,但它很可能是唯一的解决方案。有关ASP.NET MVC核心,请参阅。