今日问题:
1 。传值判断是否为空
public ActionResult EditNew(string Id) {//编辑新闻
New_Cate nc = new New_Cate();
if (!string.IsNullOrEmpty(Id))
nc = NewBLL.ListNewId(Convert.ToInt16( Id));
return View(nc);
}
2. 富文本前台传后台
当页面编辑或运行提交时,出现“从客户端中检测到有潜在危险的request.form值”问题,该怎么办呢?如下图所示:
<span style="font-size:32px;">3、同样,打开web.config配置文件,在<system.web></system.web>中间,加上代码:
<httpRuntime requestValidationMode="2.0" />
4、如果你的网站程序本身是net 2.0环境开发的,但放到了VS2010软件里运行,也会出现这种情况,你可以把运行解决方案切换成net2.0即可,更换方法详见页面第3步:点击进入
5、另一种处理方法:原因是所传值含有特殊危险符号,那么可以在传前,利用Server.HtmlEncode(string)方法,对字符串进行编码,这样就会将危险字符转义为普通的字符。如TextBox1.Text=Server.HtmlEncode(str);</span>
3 前端显示数据库中的 富文本
乱码:
好吧MVC里面要用到 @Html.Raw()
<div>@Html.Raw( @Model.NewsContent)</div>
问题解决
2015.10.19谁说解决了?
如果实在新闻列表的 一堆html代码
效果图(下):(但是富文本调出来的都是有html标签的)
SubString(0,200) 可以截取200个前面的字,但是不行啊 @html.Raw编译了一次,Substring()又变成了字符串
posted @ 2014-12-17 23:43 洞庭夕照 阅读(3853) 评论(10)
编辑
我的思路;1. 新闻插入数据库的时候,多一个列 ,用来保存 去html后的省略内容
2. 前段处理,不满足三层架构了
前端缩写:
@{
string con = hycyjyM.Other.ReplaceHtml.NoHTML(i.NewsContent);//调用正则表达式去html
if (con.Length >= 200) {
con = con.Substring(0, 200);
}
}
@con
正则去html类:
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace hycyjyM.Other
{
public class ReplaceHtml
{
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
}
}