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

时间:2010-08-25 19:02:09

标签: asp.net-mvc custom-attributes authorize-attribute

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

filterContext.ActionDescriptor.ControllerDescriptor

检查自定义属性的操作和控制器的ASP.NET MVC 1方法是什么?在ASP.NET MVC 1中,我无法告诉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 } }

5 个答案:

答案 0 :(得分:21)

更好,更可靠*方法:

filterContext.ActionDescriptor.GetCustomAttributes(
    typeof(RequireHttpsAttribute), true).Count> 0

虽然这可能只是MVC 3.0+。

答案 1 :(得分:14)

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

filterContext.HasMarkerAttribute<RequireHttpsAttribute>()

使用这组助手扩展:

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

答案 2 :(得分: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;

答案 3 :(得分:1)

这在.NET Core 2.2中对我有用:

var controllerActionDescriptor = actionContext.ActionDescriptor作为ControllerActionDescriptor;

        if (controllerActionDescriptor != null)
        {
            // Check if the attribute exists on the action method
            if (controllerActionDescriptor.MethodInfo?.GetCustomAttributes(inherit: true)?.Any(a => a.GetType().Equals(typeof(CustomAttribute))) ?? false)
                return true;

            // Check if the attribute exists on the controller
            if (controllerActionDescriptor.ControllerTypeInfo?.GetCustomAttributes(typeof(CustomAttribute), true)?.Any() ?? false)
                return true;
        }

答案 4 :(得分:0)

我正在使用MVC5,不得不使用以下内容从继承自ActionFilterAttribute并实现IAuthenticationFilter的类中进行检查。

If filterContext.ActionDescriptor.GetCustomAttributes(GetType(RequireHttpsAttribute), True).Any() OrElse filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(GetType(RequireHttpsAttribute), True).Any() Then
' .. the given attribute is present ..
End If

我无法获得Ruben的解决方案来为我工作,但这可能是因为我搞砸了从C#到VB的转换。