简体   繁体   English

HTML JavaScript编码器和C#解码器

[英]HTML JavaScript Encoder and C# Decoder

I would like to implement HTML encoder in JavaScript like this . 我想实现HTML编码器在JavaScript中像这样 And implement the corresponding decoder like this . 并实施方式与相应的解码器

Any encoder/decoder would work. 任何编码器/解码器都可以工作。 Those were just examples. 这些仅是示例。

My ultimate goal is to transfer HTML markup via a HiddenField and then use that transferred HTML on the destination. 我的最终目标是通过HiddenField传输HTML标记,然后在目标位置使用该HTML标记。 Without encoding/decoding, the page breaks. 没有编码/解码,页面就会中断。 That's why I would like to encode in JavaScript and decode in C#. 这就是为什么我想用JavaScript编码并用C#解码的原因。

JavaScript encoder: JavaScript编码器:

function htmlEncode(value) {
     //create a in-memory div, set its inner text (which jQuery automatically encodes)
     //then grab the encoded contents back out. The div never exists on the page.
     return $('<div/>').text(value).html();
}

C# decoder: C#解码器:

public string HTMLDecode(value)
{
    return HttpUtility.HtmlDecode(value);
}

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

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