代码之家  ›  专栏  ›  技术社区  ›  DavGarcia

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

  •  12
  • DavGarcia  · 技术社区  · 15 年前

    从中签出示例代码 http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc 写给ASP.NETMVC2,我注意到他们可以通过访问 filterContext.ActionDescriptor filterContext.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
        }
    }
    

    有什么问题吗ASP.NETMVC1检查自定义属性的操作和控制器的方法?在ASP.NETMVC 1没有 我看得出来。

    3 回复  |  直到 9 年前
        1
  •  21
  •   Sunday Ironfoot    12 年前

    更好更可靠的*方法:

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

    尽管这可能只是mvc3.0+版本。

        2
  •  15
  •   Ruben Bartelink    9 年前

    镀金版,适用于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>();
        }
    }
    
        3
  •  9
  •   DavGarcia    15 年前

    这似乎奏效了。。。有没有更好/更合适的方法进去ASP.NETMVC 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;
    
        4
  •  3
  •   Johan Maes Ray Pietrzak    5 年前

    var controllerActionDescriptor = actionContext.ActionDescriptor as 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;
    }
    
        5
  •  1
  •   Drew    6 年前

    我使用的是MVC5,并且必须在继承actionfilteratribute并实现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的转换过程中搞砸了。