Uri parse with # in the middle RRS feed

  • Question

  • Hi!

    I have this line of code:

    Console.WriteLine(new Uri("https://myserver.com/personal/test/Documents/Attachments/My Pictures/file with % and # in in its name.jpg").GetComponents(UriComponents.Path,UriFormat.Unescaped));

    I want to get "personal/test/Documents/Attachments/My Pictures/file with % and # in in its name.jpg" but the above code cuts it and returns "personal/test/Documents/Attachments/My Pictures/file with % and ". 

    I cannot use Uri.AbsoluteUri since I want the Unescaped string and not to replace it with escaped characters...

    is there any secure way to get what I want?


    Saturday, May 18, 2019 3:58 AM

All replies

  • The URL string after # is used for "named anchor" and traditionally not considered to be "path" part of URI.

    You'll want to just join the result back with another call with UriComponents.Fragment if you want. This should work even if you have multiple "#" sign in the path.

    However the best approach is just to escape them to "https://myserver.com/personal/test/Documents/Attachments/My Pictures/file%20with%20%25%20and%20%23%20in%20in%20its%20name.jpg", because with your approach you won't be able to access the file if it have substring like "%20" in its path - those "%nn" parts will be automatically unescaped back to the corresponding character by the web server.

    Monday, May 20, 2019 1:33 AM