ASP.Net MVC RedirectToAction with anchor

asp.netasp.net MvcFragment Identifier

asp.net Problem Overview


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

asp.net Solutions


Solution 1 - asp.net

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);

Solution 2 - asp.net

Another alternative with Url.Action:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioninikulinView Question on Stackoverflow
Solution 1 - asp.netmmxView Answer on Stackoverflow
Solution 2 - asp.netJo SmoView Answer on Stackoverflow