类似设置validateRequest="false"的方法不推荐,因为应用程序需要显式检查所有输入,不方便。

    1、前端使用encodeHtml函数对字符串进行编码,例:

var editor = $("textarea[name='editorValue']");
$("#contents").val(encodeHtml(editor.val()));
var formData = new FormData($("#frm")[0]);

    2、后端使用HtmlUtil.DecodeHtml方法进行解码,例:

model.contents = HtmlUtil.DecodeHtml(model.contents);

    3、View页面展示在编辑器中,例:

ue.addListener('ready', function (editor) {
var contents = decodeHtml("@content.contents");
ue.setContent(contents);
});
JS方法decodeHtml代码:
function encodeHtml(val) {
return val.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
}

function decodeHtml(val) {
return val.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/&#039;/g, "'")
.replace(/"/g, "\"");
}

    说明:上面的encodeHtml和decodeHtml方法有点绕人,因为这两个方法不对称,encodeHtml方法少了.replace(/'/g, "&#039;").replace(/\"/g, "&quot;")。编码的目的,是为了绕过验证,而导致验证不通过的原因是因为某些html标签,也就是说当字符串中含有<、>、&#039;之类的标识时,就会验证不通过,所以只需把&、<、>这三个替换掉就可以了。如果加上.replace(/'/g, "&#039;").replace(/\"/g, "&quot;"),则又会导致出现$#039;从而导致验证不通过,所以encodeHtml方法只替换3个。但解码多替换了两个,这是必需的,否则<span style="font-size:20px;"></span>在百度编辑器中显示会变成<span></span>从而造成style样式的丢失。

    HtmlUtil.DecodeHtml方法代码:

彻底解决“从客户端中检测到有潜在危险的Request.Form值”_字符串彻底解决“从客户端中检测到有潜在危险的Request.Form值”_字符串_02

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Utils
{
/// <summary>
/// HTML工具类
/// </summary>
public class HtmlUtil
{
#region html编码
/// <summary>
/// html编码
/// </summary>
public static string EncodeHtml(string html)
{
return html.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace("'", "&#039;")
.Replace("\"", """);
}
#endregion

#region html解码
/// <summary>
/// html解码
/// </summary>
public static string DecodeHtml(string html)
{
return html.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace("&#039;", "'")
.Replace(""", "\"");
}
#endregion

}
}

View Code