Search code examples
c#urldecodeencodeurldecode

Why Does Url Decode a Backslash into 2 Backslashes


I am trying to decode a url encoding. However, during testing I saw that the encoding of a backslash \ which is %5C will get decoded into \\. Why is that? How can I make it just one backslash without replacing it manually?

This is my code:

string decodedValue = Uri.UnescapeDataString(articleNr);

Solution

  • I think you are viewing the value of backslash in a debugger rather than printing it out. Since the backslash is a special escaping character, the debugger shows it as \\ but it's \ in the actual value.

    Console.WriteLine(Uri.UnescapeDataString("%5C")); // prints \