码迷,mamicode.com
首页 > Web开发 > 详细

MVC拦截器,MVC过滤器,MVC ActionFilterAttribute拦截器过滤器,OnActionExecuting

时间:2017-08-17 13:06:42      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:mvc拦截器   mvc过滤器   mvc actionfilterattribute拦截器过滤器   

该过滤拦截器动态拦截字符串和实体类检查是否有关键字,对字符串和动态实体类进行修改很再提交。

第一步:新的拦截器类名并继承ActionFilterAttribute  :CustomerFilterAttribute:ActionFilterAttribute  


第二步:在方法OnActionExecuting中实现


第三步:在对应的Action或者类上方加上[CustomerFilter]即可实现对该Action或者类进行拦截控制过滤。

完整代码如下:


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>

        /// 在执行操作Action方法前执行调用

        /// </summary>

        /// <param name="filterContext"></param>

        public override void OnActionExecuting(ActionExecutingContext filterContext)

        {

            base.OnActionExecuting(filterContext);

            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;

        }


    }

}


例如:如:对基类BaseController 进行控制


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]

    public class BaseController : Controller

    {

        /// <summary>

        /// 弹出成功提示

        /// </summary>

        /// <param name="message">成功消息</param>

        /// <param name="url">跳转路径</param>

        /// <returns></returns>

        protected ActionResult SuccessResult(string message, string url)

        {


            TempData["SuccessResult"] = message;

            return Redirect(url);

        }

    }

MVC拦截器,MVC过滤器,MVC ActionFilterAttribute拦截器过滤器,OnActionExecuting

标签:mvc拦截器   mvc过滤器   mvc actionfilterattribute拦截器过滤器   

原文地址:http://haihuiwei.blog.51cto.com/4789207/1956993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!