您好,登錄后才能下訂單哦!
MVC實現攔截過濾器,過濾字符串及實體類和動態修改數據,部分過濾和全部過濾:
#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; }//是否需要過濾標記
/// <summary>
/// 在執行操作Action方法前執行調用
/// </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))
{
//獲取字符串參數原值
var orginalValue = filterContext.ActionParameters[parameter.ParameterName] as string;
//使用過濾算法處理字符串
if (!string.IsNullOrEmpty(orginalValue) && orginalValue!="")
{
var filteredValue = HtmlEscapeCode(orginalValue);
////將處理后值賦給參數
filterContext.ActionParameters[parameter.ParameterName] = filteredValue;
}
}
else if (parameter.ParameterName =="model")
{
//獲取字符串參數原值
var value = filterContext.ActionParameters[parameter.ParameterName];
if (value.GetType().IsClass && value.GetType().Name != "String")//檢查是否是類,并且不是字符串類型
{
object objClass = value;//獲取字符串參數原值
PropertyInfo[] infos = objClass.GetType().GetProperties();//獲取原對象的所有公共屬性
#region 動態創建新實例【動態創建新的實體類實例】
System.Type tt = System.Type.GetType(value.ToString());//獲取指定名稱的類型
object ff = Activator.CreateInstance(tt, null);//創建指定類型實例
PropertyInfo[] fields = ff.GetType().GetProperties();//獲取指定對象的所有公共屬性
object obj = Activator.CreateInstance(tt, null);//創建新指定類型的實例【動態創建新的實例】
#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);
//將處理后值賦給參數
info.SetValue(obj, filteredValue, null);
//給實體對象賦新值
filterContext.ActionParameters[parameter.ParameterName] = obj;
}
}
else
{
object orginalValue = info.GetValue(objClass, null);//獲取值
info.SetValue(obj, orginalValue,null);//給對象賦新值
filterContext.ActionParameters[parameter.ParameterName] = obj;//給實體類對象賦值
}
}
}
}
}
}
}
/// <summary>
/// 在執行操作Action方法后執行調用
/// </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"];
}
//過濾關鍵字
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)]//不需要過濾標記
public class BugController : AuthorizeBaseController
{
/// <summary>
/// 創建BUG單管理構造函數(生成構造函數的快捷健:ctorf后按下enter健)
/// </summary>
private readonly IBugService _bugService;
public BugController(IBugService bugService)
{
_bugService = bugService;
}
}
}
//以下是需要過濾的標記
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>
/// 基礎Controller
/// </summary>
[CustomerFilter(IsCheck = true)]//過濾標簽
public class BaseController : Controller
{
/// <summary>
/// 彈出成功提示
/// </summary>
/// <param name="message">成功消息</param>
/// <param name="url">跳轉路徑</param>
/// <returns></returns>
protected ActionResult Succe***esult(string message, string url)
{
TempData["Succe***esult"] = message;
return Redirect(url);
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。