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

GSerg
  • 76,472
  • 17
  • 159
  • 346
inikulin
  • 913
  • 1
  • 8
  • 8

2 Answers2

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);
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 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? – Jacques Aug 08 '14 at 10:44
  • 1
    This solution is very difficult to test, but it may well be the only solution. – Josh Kodroff Feb 11 '15 at 19:12
24

Another alternative with Url.Action:

return Redirect(Url.Action("ShowThreadLastPostPage", "Thread", new { threadOid = threadOid }) + "last#" + postOid);
Jo Smo
  • 6,923
  • 9
  • 47
  • 67