C#URLDecode将%5C转换为\\\\而不是\ [英] C# URLDecode turns %5C into \\\\ instead of \

查看:1206
本文介绍了C#URLDecode将%5C转换为\\\\而不是\的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务方法,以网络登录(字符串)为参数。登录名是由JavaScript编码的URL,因此DOMAIN\USERNAME被编码到DOMAIN%5CUSERNAME中。当我在字符串上使用HttpUtility.URLDecode时,它会转义反斜杠并给我DOMAIN\\USERNAME。我试图将其传递给一个配置文件提供程序(它只需要一个反斜杠),并且没有任何东西。



我试图做string.Replace()以及RegEx.Replace(),似乎无法摆脱第二个反斜杠。



有没有人知道一种解决方法?现在,这只是我工作的一个概念证明,因为我不是网络用户名作为参数发布的粉丝;不过,我仍然很想知道这个问题的方法。如果我无法从C#解决这个问题,那么我可以/应该使用JavaScript编码方案吗?

解决方案

它实际上并没有变成 \\ 。这只是它在调试器下的外观。调试器会显示您要将其放入代码中时必须使用的字符串字面值。如果您将字符串打印到控制台,您会看到它很好。

  using System; 
使用System.Web;

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

Console.WriteLine(解码);
}
}

输出:

  DOMAIN\USERNAME 


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#?

解决方案

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

这篇关于C#URLDecode将%5C转换为\\\\而不是\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆