Предполагая следующие атрибуты:
using System.Web.Http.Filters;
public class CustomAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
// Does the action have the AnotherCustomAttribute attribute on it?
if (Enumerable.Any<AnotherCustomAttribute>((IEnumerable<AnotherCustomAttribute>)actionContext.ActionDescriptor.GetCustomAttributes<AnotherCustomAttribute>()))
{
// WebAPI action has your AnotherCustomAttribute attribute on it
}
base.OnActionExecuting(actionContext);
}
}
public class AnotherCustomAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
}
}
Вы можете проверить наличие одного с помощью другого:
public class HomeController : ApiController
{
public HomeController()
{
}
[CustomAttribute] // Checks for presence of 'AnotherCustomAttribute'
[AnotherCustomAttribute]
public object Get(int id)
{
return "test";
}
}
NB: используется ActionFilterAttribute
из пространства имен System.Web.Http.Filters
, а не System.Web.Mvc
.
Ваш код будет работать только в том случае, если 'ActionDescriptor' является' ReflectedHttpActionDescriptor' (т. Е. Не использовался поставщик дескрипторов пользовательских действий) и только для фильтров действий. –