3

I'm using MapPageRoute to make a route to a page, which works fine. However, I would like the page to scroll to the bottom to show a certain div with id bottom. I have tried to make the following route but the hash is being encoded in the URL so the page does not scroll down.

RouteTable.Routes.MapPageRoute("Topics", 
 "onderwerpen/{ID}/{name}#bottom", 
 "~/TopicPage.aspx"
);

results in:

mydomain/onderwerpen/1/title%23bottom

when called like this:

Response.RedirectToRoute("Topics", new { ID = 1, name = "title" });
Bazzz
  • 26,427
  • 12
  • 52
  • 69

2 Answers2

2

I think I found the most suitable solution myself. This answer is open for discussion.

string url = Page.GetRouteUrl("Topics", new { ID = 1, name = "title" });
Response.Redirect(url + "#bottom");
Bazzz
  • 26,427
  • 12
  • 52
  • 69
0

You won't be able to redirect to an anchor using Response.RedirectToRoute(). In the code you've supplied, the routeUrl parameter contains the #bottom anchor. The anchor tag doesn't belong in routeUrl because routeUrl is an expression used to match against an incoming request. And appending the #bottom anchor to the third parameter: physicalFile won't work because, as the parameter name suggests, you are specifying the name of a file on the web server, not a URL.

Why not just use good ol' Response.Redirect()?

Response.Redirect("onderwerpen/1/title#bottom");
Ivan Karajas
  • 1,081
  • 8
  • 14
  • yes that would work for now, but if later the route to the topics page is changed in `Glabal.asax` from "onderwerpen" to another string, then nobody understands why in some cases it won't redirect properly. I'd prefer a solution using the already existing route if possible. I could change the route to no longer contain the anchor, but then where do I specify it? – Bazzz Apr 29 '12 at 19:46
  • I see what you mean, but I would argue that changing the URIs of existing pages without setting up redirects is something that should be avoided if at all possible. Even if you manage to change all of the links and internal URI references on your site, you won't be able to change any external links or bookmarks. Also, there's nothing to stop you from maintaining more than one route to the same page. See Tim Berners-Lee's rant on the subject (http://www.w3.org/Provider/Style/URI.html). – Ivan Karajas Apr 30 '12 at 04:55