Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RickStrahl
Last active November 27, 2023 13:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickStrahl/44e464b7bf6feb74c814c484ed9daa46 to your computer and use it in GitHub Desktop.
Save RickStrahl/44e464b7bf6feb74c814c484ed9daa46 to your computer and use it in GitHub Desktop.
UrlEncoding on local Urls producing double encoded Uri.AbsoluteUri values
void Main()
{
var part1 = new Uri("C:\\temp\\"); // double encodes when combining parts
var part2 = "assets/Image%20File.jpg";
var uri = new Uri(part1, part2);
uri.Dump();
uri.ToString().Dump(); // still encoded and shouldn't be
uri.AbsoluteUri.Dump(); // %2520 error
uri.LocalPath.Dump(); // still encoded and shouldn't be
part1 = new Uri("file:///C:\\temp\\"); // this works as expected
uri = new Uri(part1, part2);
uri.Dump();
uri.ToString().Dump(); // raw but unencoded
uri.AbsoluteUri.Dump(); // raw and encoded
uri.LocalPath.Dump(); // unencoded as local file path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment