C# 是否与 JavaScript 的 encodeURIComponent() 等效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/86477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:27:07  来源:igfitidea点击:

Does C# have an equivalent to JavaScript's encodeURIComponent()?

提问by travis

In JavaScript:

在 JavaScript 中:

encodeURIComponent("?√") == "%C2%A9%E2%88%9A"

Is there an equivalent for C# applications? For escaping HTML characters I used:

C# 应用程序是否有等价物?为了转义我使用的 HTML 字符:

txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]",
    m => @"&#" + ((int)m.Value[0]).ToString() + ";");

But I'm not sure how to convert the match to the correct hexadecimal format that JS uses. For example this code:

但我不确定如何将匹配转换为 JS 使用的正确十六进制格式。例如这段代码:

txtOut.Text = Regex.Replace(txtIn.Text, @"[\u0080-\uFFFF]",
    m => @"%" + String.Format("{0:x}", ((int)m.Value[0])));

Returns "%a9%221a"for "?√"instead of "%C2%A9%E2%88%9A". It looks like I need to split the string up into bytes or something.

返回 " %a9%221a"for"?√"而不是"%C2%A9%E2%88%9A"。看起来我需要将字符串拆分为字节或其他内容。

Edit: This is for a windows app, the only items available in System.Webare: AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel.

编辑:这是一个Windows应用程序中,唯一可用的项目System.Web有:AspNetHostingPermissionAspNetHostingPermissionAttribute,和AspNetHostingPermissionLevel

采纳答案by Steve

Uri.EscapeDataStringor HttpUtility.UrlEncodeis the correct way to escape a string meant to be part of a URL.

Uri.EscapeDataString或者HttpUtility.UrlEncode是转义作为 URL 一部分的字符串的正确方法。

Take for example the string "Stack Overflow":

以字符串为例"Stack Overflow"

  • HttpUtility.UrlEncode("Stack Overflow")--> "Stack+Overflow"

  • Uri.EscapeUriString("Stack Overflow")--> "Stack%20Overflow"

  • Uri.EscapeDataString("Stack + Overflow")--> Also encodes "+" to "%2b"---->Stack%20%2B%20%20Overflow

  • HttpUtility.UrlEncode("Stack Overflow")--> "Stack+Overflow"

  • Uri.EscapeUriString("Stack Overflow")--> "Stack%20Overflow"

  • Uri.EscapeDataString("Stack + Overflow")--> 也编码"+" to "%2b"-->Stack%20%2B%20%20Overflow

Only the last is correct when used as an actual part of the URL (as opposed to the value of one of the query string parameters)

当用作 URL 的实际部分时,只有最后一个是正确的(与查询字符串参数之一的值相反)

回答by David Thibault

HttpUtility.HtmlEncode/ Decode
HttpUtility.UrlEncode/ Decode

HttpUtility.HtmlEncode/ 解码
HttpUtility.UrlEncode/ 解码

You can add a reference to the System.Webassembly if it's not available in your project

System.Web如果它在您的项目中不可用,您可以添加对程序集的引用

回答by Mitchel Sellers

You can use the Server object in the System.Web namespace

您可以使用 System.Web 命名空间中的 Server 对象

Server.UrlEncode, Server.UrlDecode, Server.HtmlEncode, and Server.HtmlDecode.

Server.UrlEncode、Server.UrlDecode、Server.HtmlEncode 和 Server.HtmlDecode。

Edit: poster added that this was a windows application and not a web one as one would believe. The items listed above would be available from the HttpUtility class inside System.Web which must be added as a reference to the project.

编辑:海报补充说,这是一个 Windows 应用程序,而不是人们认为的网络应用程序。上面列出的项目可以从 System.Web 中的 HttpUtility 类中获得,必须将其添加为对项目的引用。

回答by Billy Jo

Try Server.UrlEncode(), or System.Web.HttpUtility.UrlEncode()for instances when you don't have access to the Serverobject. You can also use System.Uri.EscapeUriString()to avoid adding a reference to the System.Webassembly.

Try Server.UrlEncode(),或者System.Web.HttpUtility.UrlEncode()例如当您无权访问该Server对象时。您还可以使用System.Uri.EscapeUriString()避免添加对System.Web程序集的引用。

回答by Echilon

System.Uri.EscapeUriString() didn't seem to do anything, but System.Uri.EscapeDataString() worked for me.

System.Uri.EscapeUriString() 似乎没有做任何事情,但 System.Uri.Escape DataString() 对我有用。

回答by Ali Mamedov

I tried to do full compatible analog of javascript's encodeURIComponent for c# and after my 4 hour experiments I found this

我试图为 c# 做完全兼容的 javascript 的 encodeURIComponent 模拟,经过 4 小时的实验,我发现了这一点

c# CODE:

代码:

string a = "!@#$%^&*()_+ some text here али мамедов баку";
a = System.Web.HttpUtility.UrlEncode(a);
a = a.Replace("+", "%20");

the result is: !%40%23%24%25%5e%26*()_%2b%20some%20text%20here%20%d0%b0%d0%bb%d0%b8%20%d0%bc%d0%b0%d0%bc%d0%b5%d0%b4%d0%be%d0%b2%20%d0%b1%d0%b0%d0%ba%d1%83

结果是: !%40%23%24%25%5e%26*()_%2b%20some%20text%20here%20%d0%b0%d0%bb%d0%b8%20%d0%bc% d0%b0%d0%bc%d0%b5%d0%b4%d0%be%d0%b2%20%d0%b1%d0%b0%d0%ba%d1%83

After you decode It with Javascript's decodeURLComponent();

使用 Javascript 的 decodeURLComponent() 解码后;

you will get this: !@#$%^&*()_+ some text here али мамедов баку

你会得到这个: !@#$%^&*()_+ 这里有一些文字 али мамедов баку

Thank You for attention

感谢您的关注

回答by C?ur

For a Windows Store App, you won't have HttpUtility. Instead, you have:

对于 Windows 应用商店应用程序,您将没有 HttpUtility。相反,你有:

For an URI, before the '?':

对于 URI,在 '?' 之前:

  • System.Uri.EscapeUriString("example.com/Stack Overflow++?")
    • -> "example.com/Stack%20Overflow++?"
  • 系统。Uri.EscapeUriString(“example.com/Stack Overflow++?”)
    • ->“example.com/Stack%20Overflow++?”

For an URI query name or value, after the '?':

对于 URI 查询名称或值,在“?”之后:

  • System.Uri.EscapeDataString("Stack Overflow++")
    • -> "Stack%20Overflow%2B%2B"
  • 系统。Uri.EscapeDataString("Stack Overflow++")
    • -> "堆栈%20溢出%2B%2B"

For a x-www-form-urlencodedquery name or value, in a POST content:

对于x-www-form-urlencoded查询名称或值,在 POST 内容中:

  • System.Net.WebUtility.UrlEncode("Stack Overflow++")
    • -> "Stack+Overflow%2B%2B"
  • 系统网。WebUtility.UrlEncode(“堆栈溢出++”)
    • -> "堆栈+溢出%2B%2B"