2

I am using Web Forms Routing in ASP.NET 4 and I am trying to route to a specific location on a page. On that page I have an element like <div id="3"> and I'd like to jump to this anchor from another page. For this purpose I have defined a Route in global.asax:

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/{PageAnchor}",
    "~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } });

The HyperLink to link to that page and the anchor "3" is defined this way in markup:

<asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=#3 %>">
    Link</asp:HyperLink>

The problem with the generated link is that the # character in the URL gets encoded by %23 this way: http://localhost:1234/Path/SubPath/%233 so that I reach the target page but not at the specified anchor.

Is there a way to avoid this unwished URL-encoding? Or any other way to route to an anchor?

Thank you in advance!

Slauma
  • 175,098
  • 59
  • 401
  • 420

4 Answers4

4

Anchors are not supported with ASP.NET's routing feature. Routing is designed to support only the part of the URL after the application's path and before the anchor.

I suggest adding an event handler (e.g. Page_Load) and in that event handler generate the URL, append the anchor, and set the value on the HyperLink control.

Of course, in most cases with Web Forms routing it's easiest to just set the URL manually to whatever you want. This is a nice option when the URL is not complex and is unlikely to change.

Eilon
  • 25,582
  • 3
  • 84
  • 102
  • Hm, I was hoping for a declarative solution. Well, specifying the NavigateUrl in code-behind would be the last resort. – Slauma Jun 20 '10 at 19:02
  • I came up with the same thing on my own, but found this answer when looking to see if there was any better solution. For me it was easy to do since I already was doing the links in the code behind to support my own take on route localization. Anyway, the reason I'm replying here was to provide sample code for this answer: Dim vpd As VirtualPathData = RouteTable.Routes.GetVirtualPath(Nothing, "NamedRoute", Nothing) hyperLink.NavigateUrl = vpd.VirtualPath & "#anchorName" – Daniel Flöijer Apr 24 '13 at 13:33
0

How about the route goes to a controller and that reroutes to the page with the anchor parameter?

Martea
  • 507
  • 1
  • 5
  • 18
0

Does this work?

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/#{PageAnchor}",
    "~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } })

<asp:HyperLink ID="HyperLink1" runat="server"
    NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=3 %>">
    Link</asp:HyperLink>

If you place the # outside of the PageAnchor placeholder, you could avoid that value being decoded, and it seems like a cleaner way to do it, besides.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • This is exactly what I had tried before. But it has two issues: 1) If you simply enter the route URL without an anchor in the browser (`http://server/Path/SubPath`) you get a 404 since this isn't a valid route anymore. 2) The unwished URL encoding of the `#` still happens. – Slauma Jun 20 '10 at 18:45
0

I know this is an old question, but maybe someone else could benefit from this approach.

Create a route without the anchor.

RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath", "~/MyPage.aspx");

And then construct the url like this, appending the anchor.

<a href="<%: GetRouteUrl("MyRoute", null) + "#3" %>">Link</a>
Brian
  • 41
  • 1