There are those times when you want to redirect to a route from inside your Asp.Net MVC controller and tag an anchor name (#myanchor) on the end, whilst still utilising the power of the route table. I accomplished this by creating a new class inheriting from RedirectToRouteResult and implementing the appropriate code to generate a url and then attach the required anchor.
If you’ve ever seen the source code for the standard Controller class, the code is not really anything new, but the only way it seems to extend this functionality is to almost copy the code out and mix your little extensions in there; no real hook points exist aside from that.
You use the class like so when returning an ActionResult from your controller action:
return new RedirectToActionAnchor("index", "home", "myanchor", new { id = 12 });
..which will hopefully give you a url like /home/index/12#myanchor
.
Full code snippet below!