获取ASP.NET MVC中当前操作/控制器的自定义属性列表

Dav*_*cia 14 asp.net-mvc custom-attributes authorize-attribute

查看为ASP.NET MVC2编写的http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc中的示例代码,我注意到他们可以检查自定义属性是否为通过访问filterContext.ActionDescriptorfilterContext.ActionDescriptor.ControllerDescriptor分别应用于当前操作或控制器:

public class ExitHttpsIfNotRequiredAttribute : FilterAttribute, IAuthorizationFilter {
    public void OnAuthorization(AuthorizationContext filterContext) {
        // snip

        // abort if a [RequireHttps] attribute is applied to controller or action
        if(filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return;
        if(filterContext.ActionDescriptor.GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0) return;

        // snip
    }
}
Run Code Online (Sandbox Code Playgroud)

检查自定义属性的操作和控制器的ASP.NET MVC 1方法是什么?在ASP.NET MVC 1中filterContext.ActionDescriptor,我无法分辨.

Sun*_*oot 21

更好,更可靠*方法:

filterContext.ActionDescriptor.GetCustomAttributes(
    typeof(RequireHttpsAttribute), true).Count> 0
Run Code Online (Sandbox Code Playgroud)

虽然这可能只是MVC 3.0+.

  • 这似乎没有做同样的事情.我尝试使用控制器上的属性,这返回false.使用原始问题的代码工作正常. (3认同)

Rub*_*ink 14

Goldplated版本适用于MVC5,可能是4/3:

filterContext.HasMarkerAttribute<RequireHttpsAttribute>()
Run Code Online (Sandbox Code Playgroud)

使用这组助手扩展:

public static class MarkerAttributeExtensions
{
    public static bool HasMarkerAttribute<T>(this AuthorizationContext that) {
        return that.Controller.HasMarkerAttribute<T>()
            || that.ActionDescriptor.HasMarkerAttribute<T>();
    }

    public static bool HasMarkerAttribute<T>(this ActionExecutingContext that) {
        return that.Controller.HasMarkerAttribute<T>()
            || that.ActionDescriptor.HasMarkerAttribute<T>();
    }

    public static bool HasMarkerAttribute<T>(this ControllerBase that) {
        return that.GetType().HasMarkerAttribute<T>();
    }

    public static bool HasMarkerAttribute<T>(this Type that) {
        return that.IsDefined(typeof(T), false);
    }

    public static IEnumerable<T> GetCustomAttributes<T>(this Type that) {
        return that.GetCustomAttributes(typeof(T), false).Cast<T>();
    }

    public static bool HasMarkerAttribute<T>(this ActionDescriptor that) {
        return that.IsDefined(typeof(T), false);
    }

    public static IEnumerable<T> GetCustomAttributes<T>(this ActionDescriptor that) {
        return that.GetCustomAttributes(typeof(T), false).Cast<T>();
    }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*cia 8

这似乎有用......在ASP.NET MVC 1中是否有更好/更正确的方法?

if (filterContext.Controller.GetType().GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0)
    return;
string action = (string)filterContext.RouteData.Values["action"];
if (!string.IsNullOrEmpty(action) && filterContext.Controller.GetType().GetMethod(action).GetCustomAttributes(typeof(RequireHttpsAttribute), true).Length > 0)
    return;
Run Code Online (Sandbox Code Playgroud)