ASP.NET MVC 过滤器只应用于特定命名空间

 

应用场景:后台登录验证

 

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

namespace DotNetFramework.Web.Areas.WebAdmin
{
    public class AdminLoginCheckFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            //获取命名空间
            string thisNamespace = filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace;
            //判断是否是特定命名空间
            if (!thisNamespace.Contains("DotNetFramework.Web.Areas.WebAdmin"))
            {
                return;
            }
        }
    }
}

 

//filterContext.ActionDescriptor  可以获得Action  的信息:
//获取 Action  名称
filterContext.ActionDescriptor.ActionName  
//获取 Controller 名称
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName  
//获取 Controller 的 Type 
filterContext.ActionDescriptor.ControllerDescriptor.ControllerType  


//获取 Area 名称
filterContext.RouteData.DataTokens["area"]

//获取 HttpMethod 
filterContext.HttpContext.Request.HttpMethod

//获取命名空间名称
filterContext.ActionDescriptor.ControllerDescriptor.ControllerType.Namespace

 

posted @ 2017-10-21 22:52  linyongqin  阅读(126)  评论(0编辑  收藏  举报