How to include number (hash) character # in path segment?

Viewed 1116

I have to download a file (using existing Flurl-Http endpoints [1]) whose name contains a "#" which of course has to be escaped to %23 to not conflict with uri-fragment detection.

But Flurl always escapes the rest but not this character, resulting in a non working uri where half of the path and all query params are missing because they got parsed as uri-fragment:

Url url = "http://server/api";
url.AppendPathSegment("item #123.txt");
Console.WriteLine(url.ToString());

Returns: http://server/api/item%20#123.txt

This means a http request (using Flurl.Http) would only try to download the non-existing resource http://server/api/item%20.

Even when I pre-escape the segment, the result still becomes exactly the same:

url.AppendPathSegment("item %23123.txt");
Console.WriteLine(url.ToString());

Again returns: http://server/api/item%20#123.txt.

Any way to stop this "magic" happen?

[1] This means I have delegates/interfaces where input is an existing Flurl.Url instance which I have to modify.

2 Answers

I ended up using Uri.EscapeDataString(foo) because suggested WebUtility.UrlEncode replaces space with '+' which I didn't want to.

Related