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

MVC 过滤器 ActionFilterAttribute

时间:2014-05-22 15:54:09      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:des   class   c   ext   http   a   

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);
}
}
}

MVC 过滤器 ActionFilterAttribute,布布扣,bubuko.com

MVC 过滤器 ActionFilterAttribute

标签:des   class   c   ext   http   a   

原文地址:http://www.cnblogs.com/sumg/p/3744054.html

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