Get list of custom attributes for current action/controller in ASP.NET MVC
Checking out the sample code from http://lukesampson.com/post/471548689/entering-and-exiting-https-with-asp-net-mvc written for ASP.NET MVC2, I noticed they can check if a custom attribute is applied to the current action or controller by accessing filterContext.ActionDescriptor
and filterContext.ActionDescriptor.ControllerDescriptor
respectively:
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
}
}
What would be the ASP.NET MVC 1 method of checking the action and controller for a custom attribute? In ASP.NET MVC 1 there is no filterContext.ActionDescriptor
that I can tell.
Even better and more reliable* approach:
filterContext.ActionDescriptor.GetCustomAttributes(
typeof(RequireHttpsAttribute), true).Count> 0
Though this might be MVC 3.0+ only.
Goldplated edition, works on MVC5, probably 4/3:
filterContext.HasMarkerAttribute<RequireHttpsAttribute>()
Uses this set of helper extensions:
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>();
}
}
This seems to work... is there a better / more proper way in 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;
this worked for me in .NET Core 2.2:
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;
}
I'm using MVC5 and had to use the following to check from within a class that inherits from ActionFilterAttribute and implements 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
I couldn't get Ruben's solution to work for me but it was probably because I messed up in the conversion from C# to VB.
精彩评论