MVC 过滤器 ActionFilterAttribute
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Web;
using System.Web.Mvc;
using
过滤器.Controllers;
namespace 过滤器.Filtes
{
public class
MyActionFiterAttribute:ActionFilterAttribute
{
/// <summary>
/// Action过滤器 执行方法之前
///
</summary>
/// <param
name="filterContext"></param>
public override void
OnActionExecuting(ActionExecutingContext filterContext)
{
//RouteData保存了当前请求的路由信息
// filterContext.RouteData.Values["action"];
// filterContext.RouteData.DataTokens["area"]; 获取区域名称
if
(filterContext.ActionDescriptor.IsDefined(typeof(MyActionFiterAttribute),
false))//检查当前请求的action是否有贴MyActionFiterAttribute标签
{
//不执行Action方法,也不执行OnActionExecuted方法,会执行Result过滤器
filterContext .Result=new
ContentResult ();//给当前请求设置返回结果,不会再继续执行当前的action
}
filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnActionExecuting");
base.OnActionExecuting(filterContext);
}
//执行方法之后
public override void OnActionExecuted(ActionExecutedContext
filterContext)
{
filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnActionExecuted");
base.OnActionExecuted(filterContext);
}
//Result过滤器
public override void OnResultExecuted(ResultExecutedContext
filterContext)
{
filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnResultExecuted");
base.OnResultExecuted(filterContext);
}
public
override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Write("哈哈哈哈哈~!!!OnResultExecuting");
base.OnResultExecuting(filterContext);
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。