C# URLDecode turns %5C into \\\\ instead of \

醉酒当歌 提交于 2019-12-25 07:01:25

问题


I have a web service method that takes a network login (string) as a parameter. The login is URL Encoded from JavaScript so the DOMAIN\USERNAME is encoded into DOMAIN%5CUSERNAME. When I use the HttpUtility.URLDecode on the string, it escapes the backslash and gives me DOMAIN\\USERNAME. I'm attempting to pass this into a profile provider (which expects just a single backslash) and am getting nothing.

I've attempted to do string.Replace() as well as RegEx.Replace() and can't seem to get rid of the 2nd backslash.

Does anyone know a way to resolve this? For now, this is just a proof of concept since I'm working on since I'm not a fan of the network username being posted as a parameter; however, I am still curious to know a way around this issue. Is there a different encoding scheme I can/should use on the JavaScript side if I can't resolve this from C#?


回答1:


It doesn't actually turn it into \\. That's just how it looks under the debugger. The debugger shows you the string literal you'd have to use if you wanted to put it in code. If you print the string out to the console you'll see it's fine.

using System;
using System.Web;

class Test
{
    static void Main()
    {
        string url = "DOMAIN%5CUSERNAME";
        string decoded = HttpUtility.UrlDecode(url);

        Console.WriteLine(decoded);
    }
}

Output:

DOMAIN\USERNAME


来源:https://stackoverflow.com/questions/608453/c-sharp-urldecode-turns-5c-into-instead-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!