C#URLDecode将%5C转换为\\\\而不是\ [英] C# URLDecode turns %5C into \\\\ instead of \
问题描述
我试图做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屋!