Thursday, May 5, 2011

Accessing Jump Links (the part of the URL after a hasch character, #) from the code behind

Hi!

Anyone know if it's possible to access the name of a jump link in c# code?

I'm doing some url Rewriting stuff and I'm thinking I might not be able to see that part of the URL.

So basically, my URL looks a little something like this:

http://www.mysite.com/Terms.aspx#Term1

And I want to access "Term1". I can't see it in the ServerVariables...

Any ideas?!?!?

THANKS!

From stackoverflow
  • Perhaps System.Uri.Fragment? Or what is it you don't see?

    Y Low : System.Uri.Fragment will only contain the fragment if the url contains one, when an HTTP request is made the fragment part is not submitted to the server, that's why this won't work
    Pat : +1 Initialize a Uri object and you should be able to access the fragment.
    Y Low : No, he wont. The fragment is never submitted to the server by the web browser. The fragment property will only show the fragment if it was initialized by a url that includes a fragment.
    Ev : Great! Thanks man. That's really close to what I'm after. So if I hardcode: System.Uri uri = new Uri("http://www.mysite.com/Terms.aspx#Term1"); then go uri.Fragment I get "#Term1" which is exactly what I'm after. The problem is, the URL should be the current location of the page. How can I programatically get that URI, including what is after the hash character.
    Ev : @Y Low: thanks a lot - sounds like you know what I'm talking about here. If the fragment is not submitted to the server, I'll never be able to access it. Can you (or anyone) confirm this for me?
    Y Low : 1) You wont get '#Term1', You'll get 'Term1' (the # is not part of the fragment) 2) This will not work if the url comes from a client request (since client requests never submit the fragment)
    Y Low : Check the RFC specification or the link I provided in the answer below
  • The hash character is meant for client side navigation. Anything after the # is not submitted to the server.

    From the wikipedia article:

    The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server. When an agent (such as a Web browser) requests a resource from a Web server, the agent sends the URI to the server, but does not send the fragment.

    Its technical name is Fragment Identifier

    Ev : That's the answer. Thanks Y Low. Cheers @ Pontus Gagge too. Nice one guys.

0 comments:

Post a Comment