简体   繁体   English

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

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

In JavaScript: 在JavaScript中:

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

Is there an equivalent for C# applications? 是否有C#应用程序的等价物? For escaping HTML characters I used: 对于我使用的转义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. 但我不确定如何将匹配转换为JS使用的正确十六进制格式。 For example this code: 例如这段代码:

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" . 返回“ %a9%221a"代表"©√"而不是"%C2%A9%E2%88%9A" It looks like I need to split the string up into bytes or something. 看起来我需要将字符串拆分为字节或其他内容。

Edit: This is for a windows app, the only items available in System.Web are: AspNetHostingPermission , AspNetHostingPermissionAttribute , and AspNetHostingPermissionLevel . 编辑:这是针对Windows应用程序, System.Web中唯一可用的项目是: AspNetHostingPermissionAspNetHostingPermissionAttributeAspNetHostingPermissionLevel

Uri.EscapeDataString or HttpUtility.UrlEncode is the correct way to escape a string meant to be part of a URL. Uri.EscapeDataStringHttpUtility.UrlEncode是转义意味着成为URL一部分的字符串的正确方法。

Take for example the string "Stack Overflow" : 以字符串"Stack Overflow"为例:

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

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

  • Uri.EscapeDataString("Stack + Overflow") --> Also encodes "+" to "%2b" ----> Stack%20%2B%20%20Overflow Uri.EscapeDataString("Stack + Overflow") - > 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的实际部分时,只有最后一个是正确的(而不是查询字符串参数之一的值)

HttpUtility.HtmlEncode / Decode HttpUtility.HtmlEncode / Decode
HttpUtility.UrlEncode / Decode HttpUtility.UrlEncode / Decode

You can add a reference to the System.Web assembly if it's not available in your project 如果在项目中不可用,则可以添加对System.Web程序集的引用

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: c#代码:

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%是%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 感谢您的关注

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

Try Server.UrlEncode() , or System.Web.HttpUtility.UrlEncode() for instances when you don't have access to the Server object. 当您无权访问Server对象时,请尝试使用Server.UrlEncode()System.Web.HttpUtility.UrlEncode() You can also use System.Uri.EscapeUriString() to avoid adding a reference to the System.Web assembly. 您还可以使用System.Uri.EscapeUriString()来避免添加对System.Web程序集的引用。

For a Windows Store App, you won't have HttpUtility. 对于Windows应用商店应用,您将没有HttpUtility。 Instead, you have: 相反,你有:

For an URI, before the '?': 对于URI,在'?'之前:

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

For an URI query name or value, after the '?': 对于URI查询名称或值,在'?'之后:

  • System. 系统。 Uri.EscapeDataString ("Stack Overflow++") Uri.EscapeDataString (“Stack Overflow ++”)
    • -> "Stack%20Overflow%2B%2B" - >“Stack%20Overflow%2B%2B”

For a x-www-form-urlencoded query name or value, in a POST content: 对于x-www-form-urlencoded查询名称或值,在POST内容中:

  • System.Net. System.Net。 WebUtility.UrlEncode ("Stack Overflow++") WebUtility.UrlEncode (“Stack Overflow ++”)
    • -> "Stack+Overflow%2B%2B" - >“Stack + Overflow%2B%2B”

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. 编辑:海报补充说,这是一个Windows应用程序而不是人们会相信的网络应用程序。 The items listed above would be available from the HttpUtility class inside System.Web which must be added as a reference to the project. 上面列出的项目可以从System.Web中的HttpUtility类获得,该类必须作为项目的引用添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM