标签:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new logFilterAttribute()); //这一行是新加入的
}
在FilterConfig中添加新的覆写过滤规则
覆写OnActionExecuting方法,然后在Controller的action中只要添加 [logFilter] 属性就可以直接使用过滤器方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class logFilterAttribute : ActionFilterAttribute { // 在方法执行之前拦截 public override void OnActionExecuting(ActionExecutingContext filterContext) { //跳转 登录页面 if (filterContext.HttpContext.Session[ "UserName" ] == null ) { filterContext.Result = new RedirectToRouteResult( new RouteValueDictionary { { "Controller" , "Account" }, { "Action" , "Login" } }); } base .OnActionExecuting(filterContext); } } |
1 2 3 4 5 | [Authorize] public ActionResult TestAuthorize() { return View(); } |
标签:
原文地址:http://www.cnblogs.com/weloveshare/p/5309112.html