C# MVC过滤器属性-多个选项作为过滤器参数

C# MVC过滤器属性-多个选项作为过滤器参数,c#,asp.net-mvc-4,action-filter,C#,Asp.net Mvc 4,Action Filter,嗨,我的C 35;控制器有一个自定义过滤器属性 [SessionExpireFilterAttribute(UserType = UserTypes.Admin)] 我的过滤器是这样启动的: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)] public class SessionExpireFilterAttribute

嗨,我的
C 35;
控制器有一个自定义过滤器属性

 [SessionExpireFilterAttribute(UserType = UserTypes.Admin)]
我的过滤器是这样启动的:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class SessionExpireFilterAttribute : ActionFilterAttribute
    {
        public App.Model.UserTypes UserType { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;
[SessionExpireFilterAttribute(UserType = UserTypes.Admin || UserTypes.Accounting || UserTypes.Standard)]
public App.Model.UserTypes[] UserTypes { get; set; }

public SessionExpireFilterAttribute(params App.Model.UserType[] values) {
    UserTypes = values;
}
[SessionExpireFilterAttribute(UserTypes.Admin, UserTypes.Accounting, UserTypes.Standard)]
我想要的是能够发送多个
用户类型
,因此此控制器的权限不仅仅与一个相关

例如,要设置如下内容:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class SessionExpireFilterAttribute : ActionFilterAttribute
    {
        public App.Model.UserTypes UserType { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;
[SessionExpireFilterAttribute(UserType = UserTypes.Admin || UserTypes.Accounting || UserTypes.Standard)]
public App.Model.UserTypes[] UserTypes { get; set; }

public SessionExpireFilterAttribute(params App.Model.UserType[] values) {
    UserTypes = values;
}
[SessionExpireFilterAttribute(UserTypes.Admin, UserTypes.Accounting, UserTypes.Standard)]
这样,如果用户类型是
Admin
Accounting
Standard
,我可以在过滤器中执行逻辑,让流程继续

有线索吗


您可以使用数组进行此操作,如下所示:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class SessionExpireFilterAttribute : ActionFilterAttribute
    {
        public App.Model.UserTypes UserType { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;
[SessionExpireFilterAttribute(UserType = UserTypes.Admin || UserTypes.Accounting || UserTypes.Standard)]
public App.Model.UserTypes[] UserTypes { get; set; }

public SessionExpireFilterAttribute(params App.Model.UserType[] values) {
    UserTypes = values;
}
[SessionExpireFilterAttribute(UserTypes.Admin, UserTypes.Accounting, UserTypes.Standard)]
然后你可以这样使用它:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class SessionExpireFilterAttribute : ActionFilterAttribute
    {
        public App.Model.UserTypes UserType { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpContext ctx = HttpContext.Current;
[SessionExpireFilterAttribute(UserType = UserTypes.Admin || UserTypes.Accounting || UserTypes.Standard)]
public App.Model.UserTypes[] UserTypes { get; set; }

public SessionExpireFilterAttribute(params App.Model.UserType[] values) {
    UserTypes = values;
}
[SessionExpireFilterAttribute(UserTypes.Admin, UserTypes.Accounting, UserTypes.Standard)]
注意:您不必使用
params
,您也可以这样做

[SessionExpireFilterAttribute(new [] {UserTypes.Admin, UserTypes.Accounting, UserTypes.Standard})]

谢谢,谢谢