首页 技术 正文
技术 2022年11月6日
0 收藏 693 点赞 519 浏览 1690 个字

类似设置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, "&lt;")
.replace(/>/g, "&gt;");
}function decodeHtml(val) {
return val.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/'/g, "'")
.replace(/&quot;/g, "\"");
}

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

HtmlUtil.DecodeHtml方法代码:

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("&", "&amp;")
.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("'", "'")
.Replace("\"", "&quot;");
}
#endregion #region html解码
/// <summary>
/// html解码
/// </summary>
public static string DecodeHtml(string html)
{
return html.Replace("&amp;", "&")
.Replace("&lt;", "<")
.Replace("&gt;", ">")
.Replace("'", "'")
.Replace("&quot;", "\"");
}
#endregion }
}

    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289