Skip to content

Instantly share code, notes, and snippets.

@lfgrando
Last active July 20, 2018 18:16
Show Gist options
  • Save lfgrando/9febb1adbaaaa3a5d883ccb60f1bae38 to your computer and use it in GitHub Desktop.
Save lfgrando/9febb1adbaaaa3a5d883ccb60f1bae38 to your computer and use it in GitHub Desktop.
Goldplated edition, works on MVC5, probably 4/3:filterContext.HasMarkerAttribute<RequireHttpsAttribute>()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Core.Extensions
{
public static class MarkerAttributeExtension
{
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>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment