ASP.Net MVC RedirectToAction with anchor
Asked Answered
S

2

85

I have the following problem: For example I have route like this:

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

Is there a way using RedirectToAction method navigate to the URL like this:

forums/thread/{threadOid}/last#postOid

Stalkinghorse answered 2/3, 2009 at 15:39 Comment(1)
For ASP.NET MVC Core, see https://mcmap.net/q/226118/-redirect-to-div-in-page-with/11683.Woodcut
M
157

I think you should use the Redirect method along with Url.RouteUrl to accomplish it.

return Redirect(Url.RouteUrl(new { controller = "Thread", action = "ShowThreadLastPostPage", threadOid = threadId }) + "#" + postOid);
Monoculture answered 2/3, 2009 at 15:44 Comment(2)
Like this answer, but doesn't this cause a render to the client then a new request to the server followed by another render? If so, is there another way to avoid this?Riposte
This solution is very difficult to test, but it may well be the only solution.Christly
P
24

Another alternative with Url.Action:

return Redirect(Url.Action("ShowThreadLastPostPage", "Thread", new { threadOid = threadOid }) + "last#" + postOid);
Puerility answered 4/2, 2016 at 0:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.