您好,登錄后才能下訂單哦!
MVC實(shí)現(xiàn)攔截過濾器,過濾字符串及實(shí)體類和動(dòng)態(tài)修改數(shù)據(jù),部分過濾和全部過濾:
#region
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Reflection;
namespace SaaS.Admin.Base
{
/// <summary>
/// 全局過濾器
/// </summary>
public class CustomerFilterAttribute:ActionFilterAttribute
{
/// <summary>
/// 檢查是否需要過濾
/// </summary>
public bool IsCheck { get; set; }//是否需要過濾標(biāo)記
/// <summary>
/// 在執(zhí)行操作Action方法前執(zhí)行調(diào)用
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
#region 檢查是否需要攔截過濾【不需要檢查過濾】
if (!IsCheck)
{
return;//不需要過濾
}
#endregion
var parameters = filterContext.ActionDescriptor.GetParameters();
foreach (var parameter in parameters)
{
if (parameter.ParameterType == typeof(string))
{
//獲取字符串參數(shù)原值
var orginalValue = filterContext.ActionParameters[parameter.ParameterName] as string;
//使用過濾算法處理字符串
if (!string.IsNullOrEmpty(orginalValue) && orginalValue!="")
{
var filteredValue = HtmlEscapeCode(orginalValue);
////將處理后值賦給參數(shù)
filterContext.ActionParameters[parameter.ParameterName] = filteredValue;
}
}
else if (parameter.ParameterName =="model")
{
//獲取字符串參數(shù)原值
var value = filterContext.ActionParameters[parameter.ParameterName];
if (value.GetType().IsClass && value.GetType().Name != "String")//檢查是否是類,并且不是字符串類型
{
object objClass = value;//獲取字符串參數(shù)原值
PropertyInfo[] infos = objClass.GetType().GetProperties();//獲取原對象的所有公共屬性
#region 動(dòng)態(tài)創(chuàng)建新實(shí)例【動(dòng)態(tài)創(chuàng)建新的實(shí)體類實(shí)例】
System.Type tt = System.Type.GetType(value.ToString());//獲取指定名稱的類型
object ff = Activator.CreateInstance(tt, null);//創(chuàng)建指定類型實(shí)例
PropertyInfo[] fields = ff.GetType().GetProperties();//獲取指定對象的所有公共屬性
object obj = Activator.CreateInstance(tt, null);//創(chuàng)建新指定類型的實(shí)例【動(dòng)態(tài)創(chuàng)建新的實(shí)例】
#endregion
foreach (PropertyInfo info in infos)
{
if (info.CanRead)
{
//Console.WriteLine(info.Name + "=" + info.GetValue(objClass, null));
if (info.PropertyType.Name == "String")
{
//獲取值
string orginalValue =Convert.ToString(info.GetValue(objClass, null));
if (!string.IsNullOrEmpty(orginalValue) || orginalValue!="")
{
//檢查過濾特殊字符
var filteredValue = HtmlEscapeCode(orginalValue);
//將處理后值賦給參數(shù)
info.SetValue(obj, filteredValue, null);
//給實(shí)體對象賦新值
filterContext.ActionParameters[parameter.ParameterName] = obj;
}
}
else
{
object orginalValue = info.GetValue(objClass, null);//獲取值
info.SetValue(obj, orginalValue,null);//給對象賦新值
filterContext.ActionParameters[parameter.ParameterName] = obj;//給實(shí)體類對象賦值
}
}
}
}
}
}
}
/// <summary>
/// 在執(zhí)行操作Action方法后執(zhí)行調(diào)用
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var controllerName = filterContext.RouteData.Values["controller"];
var actionName = filterContext.RouteData.Values["action"];
}
//過濾關(guān)鍵字
public string HtmlEscapeCode(string html)
{
var strhtml = html.Replace("javascript", "")
.Replace("vbscript", "")
.Replace("jscript", "")
.Replace("script", "")
.Replace("eval", "")
.Replace("<", "<")
.Replace(">", ">")
.Replace("\'", "'")
.Replace("\"", """)
.Replace("&", "&")
.Replace("#", "#");
return strhtml;
}
}
}
#endregion
//以下是不需要過濾的Controllers
using SaaS.Contracts.SaaS.Intern.Dtos.BugDtos;
using SaaS.Framework.SharpArch.Repositorys;
using SharpArch.NHibernate.Web.Mvc;
using SaaS.Models.Domain.Enums;
using SaaS.Models.Framework.Utility;
using SaaS.Framework.Collections;
namespace SaaS.Admin.Controllers
{
/// <summary>
/// BUG單管理
/// </summary>
[CustomerFilter(IsCheck =false)]//不需要過濾標(biāo)記
public class BugController : AuthorizeBaseController
{
/// <summary>
/// 創(chuàng)建BUG單管理構(gòu)造函數(shù)(生成構(gòu)造函數(shù)的快捷?。篶torf后按下enter健)
/// </summary>
private readonly IBugService _bugService;
public BugController(IBugService bugService)
{
_bugService = bugService;
}
}
}
//以下是需要過濾的標(biāo)記
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using SaaS.Contracts.SaaS.Intern;
using SaaS.Framework.IIdentity;
using SaaS.Models.Domain.Enums;
namespace SaaS.Admin.Base
{
/// <summary>
/// 基礎(chǔ)Controller
/// </summary>
[CustomerFilter(IsCheck = true)]//過濾標(biāo)簽
public class BaseController : Controller
{
/// <summary>
/// 彈出成功提示
/// </summary>
/// <param name="message">成功消息</param>
/// <param name="url">跳轉(zhuǎn)路徑</param>
/// <returns></returns>
protected ActionResult Succe***esult(string message, string url)
{
TempData["Succe***esult"] = message;
return Redirect(url);
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。