简体   繁体   English

编码和解码字符串以保持不变的文化

[英]Encode and decode a string to invariant culture

I have a hiddenfield in which i am storing storing the user input and code generated value in string format. 我有一个hiddenfield,我正在其中存储以字符串格式存储用户输入和代码生成的值。 Since the string may contain the hyperlink etc i used HTmlEncode before putting it into a hidden field and used to decode the value when i am retrieving from the hiddenfield. 由于字符串可能包含超链接等,因此我在将其放入隐藏字段之前使用了HTmlEncode,并在我从隐藏字段检索时用于对值进行解码。 If the user input contain French characters then the code will crash. 如果用户输入包含法语字符,则代码将崩溃。 How to encode a string containg- links,french and other culture characters. 如何编码包含链接,法语和其他区域性字符的字符串。 I am getting the below error 我收到以下错误

A potentially dangerous Request.Form value was detected from the client (ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$EmailHiddenField="...434] - [Exécuter la macro...").

Well.. If you are using c# then here is my try.. 好吧..如果您使用的是C#,那么这是我的尝试。

Encode the whole HTML document to Base64 string and then use URLEncode. 将整个HTML文档编码为Base64字符串,然后使用URLEncode。

byte[] bytesToEncode = Encoding.UTF8.GetBytes(myHTMLDocAsString);
string encodedText = Convert.ToBase64String(bytesToEncode);

and then URLEncode(encodedText) 然后是URLEncode(encodedText)

This could be successfully transfered over HTTP as POST without errors! 这可以成功地通过HTTP作为POST正确传输!

Now in case you required to store it in hidden field then instead of POSTing, just enter it into the hidden field.. 现在,如果您需要将其存储在隐藏字段中,而不是POSTing,只需将其输入到隐藏字段中即可。

Hope it helps.. 希望能帮助到你..

Use globalization tag as: 将全球化标签用作:

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="nb-no" uiCulture="no"/>

Further follow this answer you will come to know encode-decode of these strings: 进一步遵循此答案,您将了解这些字符串的编码解码:

Getting U+fffd/65533 instead of special character from Query String 从查询字符串获取U + fffd / 65533而不是特殊字符

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

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