23

I have a URL like so:

http://localhost/place/663828/bangkok-paradise-restaurant-toronto#r306040

I am trying to see if there's the existence of the anchor tag along with getting its value to do some code logic in the code behind.

I have been trying to use the Page.Request, but none of the properties show the anchor link portion of the URL.

For example:

Response.Write(this.Page.Request.RawUrl.ToString());

I pretty much tried the combinations/properties on this page: http://www.west-wind.com/weblog/posts/269.aspx

Just to finalize this topic:

I copied Stack Overflow's approach with a permalink... :D

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TimLeung
  • 3,459
  • 6
  • 42
  • 59

3 Answers3

30

It's not possible to retrieve the #anchor from the server side in ASP.NET.

This is a client-side flag to tell the browser to move to a specific place within the page.

You can use some JavaScript code in the body onLoad event to check for an anchor and send it back to the server using Ajax.

var anchorValue;
var url = document.location;
var strippedUrl = url.toString().split("#");
if(strippedUrl.Length > 1)
    anchorvalue = strippedUrl[1];

Ref: Retrieving the anchor value from a URL

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
17

Being more explicit, the anchor tag is never sent as part of the HTTP request by any browser. It is only interpreted locally within the browser. Neither ASP.NET nor any other web server technology, Microsoft or otherwise will see the anchor on that request.

RFC 1808 Section 2.4.1 - "Note that the fragment identifier is not considered part of the URL."

As others have suggested, the nearest you could get would be using client-side to read the browser window location.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stephbu
  • 5,072
  • 26
  • 42
  • 1
    Well, you're not completely right.. See: [http://www.php.net/manual/en/function.parse-url.php] – Mike Dinescu Aug 14 '09 at 19:44
  • 12
    Miky just because someone wrote a piece of code to parse anchors out of URLs - still doesn't mean it is sent by any browser over the wire. Try it - use your favourite network monitor of choice to watch the wire. You won't see any anchor in the HTTP request. – stephbu Aug 15 '09 at 03:04
  • You're still not completely right. The RFC that you linked is for URLs, not URIs. -- Fragment is a well defined part of a URI (see: https://tools.ietf.org/html/rfc3986#section-3.5), and obviously the OP wants the fragment of the URI, not the URL. You can be as pedantic as you want, but at the end of the day, it's not helping anyone. – BrainSlugs83 Oct 26 '18 at 23:36
  • From the Op's question "I have an url", "getting it's value to do some code logic in the code behind" Agree anchors are part of a URI. Would you agree that browsers send URLs only, don't send anchors on HTTP requests? Not trying to be pendantic, doing this server-side is not part of the spec, it just doesn't work that way. – stephbu Oct 29 '18 at 21:51
8

A fragment can be parsed from a URL in C# in the following way:

var uri = new Uri("http://localhost?id=2#token=23");
var fragment = uri.Fragment; // Will return #token=23

There is a problem however in that the browser won't send fragments to the server. If you receive requests from a service that includes this information in the request, it will be available from the server side too.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eugeniu Torica
  • 7,484
  • 12
  • 47
  • 62
  • This does not answer the question. A fragment can be "accessed" but this doesn't help the OP, since they wanted to access the fragment sent with the request. But as we all know, this is impossible, since it is never sent with a request to the server. The only purpose to "accessing" it would be if you had already set it. – John Washam Sep 17 '13 at 19:50
  • 1
    In this case it doesn't work but sometimes url can be sent to server side not only via browser and in that case knowing how to extract fragment via BCL helps. – Eugeniu Torica Sep 17 '13 at 20:31