MVC中如何自定义过滤器

一、自定义过滤器:public class MyActionFilterAttribute:ActionFilterAttribute,并继承ActionFilterAttribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcValidateDemo.Models
{
    [AttributeUsage(AttributeTargets.All,AllowMultiple=true)] //如果加上这个属性,那么所有加上过滤器的地方都执行,包括:Globle
    public class MyActionFilterAttribute:ActionFilterAttribute
    {
        public string name { get; set; }
        //执行之前
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            HttpContext.Current.Response.Write("<br />OnActionExecuting:"+name);
        }
        //执行之后
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            HttpContext.Current.Response.Write("<br />OnActionExecuted:" + name);
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
            HttpContext.Current.Response.Write("<br />OnResultExecuting:" + name);
        }
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
            HttpContext.Current.Response.Write("<br />OnResultExecuted:" + name);
        }
    }
}

二、在App_Start文件夹中的FilterConfig文件中注册自定义过滤器

using MvcValidateDemo.Models;
using System.Web;
using System.Web.Mvc;

namespace MvcValidateDemo
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new MyExctionFilterAttribute());
            //全局过滤器优先级最低,但是可以对所有方法起作用
            filters.Add(new MyActionFilterAttribute() { name = "Global" });
        }
    }
}

三、使用方法:
1、在控制器上使用,即在HomeController中,加入属性[MyActionFilter(name = “HomeController”)],注意:name = "HomeController"是标签。
2、在方法中使用,即在方法中加入属性[MyActionFilter(name=“Index Action”)]。
3、优先级:越靠近ActionResult优先级越高,在实际使用过程中可以选择使用,不用全部都设置。

using MvcValidateDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcValidateDemo.Controllers
{
    [MyActionFilter(name = "HomeController")]
    public class HomeController : Controller
    {
       
        [MyActionFilter(name="Index Action")]
        public ActionResult Index()
        {
            Response.Write("<p>Action被执行了!</p>");
            return Content("<br />Ok!视图被渲染!<br />");
            
        }
         [MyActionFilter(name = "about Action")]
        public ActionResult about()
        {
            throw new Exception("demo");
            //return Content("<br />about渲染了!<br />");

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值