Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode a path that contains a hash?

Tags:

c#

url

uri

How do you properly encode a path that includes a hash (#) in it? Note the hash is not the fragment (bookmark?) indicator but part of the path name.

For example, if there is a path like this:

http://www.contoso.com/code/c#/somecode.cs

It causes problems when you for example try do this:

Uri myUri = new Uri("http://www.contoso.com/code/c#/somecode.cs");

It would seem that it interprets the hash as the fragment indicator.

It feels wrong to manually replace # with %23. Are there other characters that should be replaced? There are some escaping methods in Uri and HttpUtility but none seem to do the trick.

like image 925
Dodgyrabbit Avatar asked Feb 16 '12 21:02

Dodgyrabbit


People also ask

How do you escape a hashtag URL?

Replace the hash with %23 . # is a valid URI character, but it starts the hash fragment, so you need to encode it in the query string.

What is %20 URL encode?

URL Encoding (Percent Encoding) URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

What is the difference between encodeURI and encodeURIComponent?

The difference between encodeURI and encodeURIComponent is encodeURIComponent encodes the entire string, where encodeURI ignores protocol prefix ('http://') and domain name. encodeURIComponent is designed to encode everything, where encodeURI ignores a URL's domain related roots.


2 Answers

There are a few characters you are not supposed to use. You can try to work your way through this very dry documentation, or refer to this handy URL summary on Stack Overflow.

If you check out this very website, you'll see that their C# questions are encoded %23.

Stack Overflow C# Questions

You can do this using either (for ASP.NET):

string.Format("http://www.contoso.com/code/{0}/somecode.cs", 
    Server.UrlEncode("c#")
);

Or for class libraries / desktop:

string.Format("http://www.contoso.com/code/{0}/somecode.cs",
    HttpUtility.UrlEncode("c#")
);
like image 118
Fenton Avatar answered Oct 13 '22 21:10

Fenton


Did some more digging friends and found a duplicate question for Java: HTTP URL Address Encoding in Java

However, the .Net Uri class does not offer the constructor we need, but the UriBuilder does.

So, in order to construct a proper URI where the path contains illegal characters, do this:

// Build Uri by explicitly specifying the constituent parts. This way, the hash is not confused with fragment identifier
UriBuilder uriBuilder = new UriBuilder("http", "www.contoso.com", 80, "/code/c#/somecode.cs");

Debug.WriteLine(uriBuilder.Uri);
// This outputs: http://www.contoso.com/code/c%23/somecode.cs

Notice how it does not unnecessarily escape parts of the URI that does not need escaping (like the :// part) which is the case with HttpUtility.UrlEncode. It would seem that the purpose of this class is actually to encode the querystring/fragment part of the URL - not the scheme or hostname.

like image 21
Dodgyrabbit Avatar answered Oct 13 '22 21:10

Dodgyrabbit