Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url fragment is empty

Using ASP.NET MVC3, the url http://localhost:22713/tests#123456 with the following code:

Your user agent: @Request.UserAgent<br />
Url: @Request.Url.AbsoluteUri<br />
Url fragment: @Request.Url.Fragment<br />

returns:

Your user agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16 
Url: http://localhost:22713/tests 
Url fragment:

Why is fragment always empty? I need to be able to parse this info on the server side.

like image 523
Chris Avatar asked Apr 06 '11 18:04

Chris


People also ask

What is a fragment in a URL?

A fragment is an internal page reference, sometimes called a named anchor. It usually appears at the end of a URL and begins with a hash (#) character followed by an identifier. It refers to a section within a web page. In HTML documents, the browser looks for an anchor tag with a name attribute matching the fragment.

Why fragment is used in URL?

A hash sign (#) in a URL is referred to as a fragment. Historically, URL fragments have been used to automatically set the browser's scroll position to a predefined location in the web page. In that sense, if a URL refers to a document, then the fragment refers to a specific subsection of that document.

Is URL fragment sent to server?

Fragment identifiers are not sent to the server. The hash fragment is used by the browser to link to elements within the same page.

Can a URL have multiple fragments?

A URL cannot have more than one fragment. URL parameters are passed in key-value pairs. URL fragments comprise just a string of text after the hash (#).


1 Answers

The fragment (everything after the # in a url) doesn't get passed to the server. So the Fragment property will always be empty when you attempt to get it from a request.

The Fragment property is typically only used when constructing URLs.

There's no easy way to get the fragment on the server. Typically you would have to use javascript to retrieve the fragment.

like image 181
Keltex Avatar answered Nov 15 '22 07:11

Keltex