Основываясь на этой статье Я пытаюсь создать реализацию IActionFilter
для ядра ASP.NET, которая может обрабатывать атрибуты, помеченные на контроллер и действие контроллера. Хотя чтение атрибутов контроллера легко, я не могу найти способ прочитать атрибуты, определенные в методе действий.
Вот код, который у меня есть прямо сейчас:
public sealed class ActionFilterDispatcher : IActionFilter
{
private readonly Func<Type, IEnumerable> container;
public ActionFilterDispatcher(Func<Type, IEnumerable> container)
{
this.container = container;
}
public void OnActionExecuting(ActionExecutingContext context)
{
var attributes = context.Controller.GetType().GetCustomAttributes(true);
attributes = attributes.Append(/* how to read attributes from action method? */);
foreach (var attribute in attributes)
{
Type filterType = typeof(IActionFilter<>).MakeGenericType(attribute.GetType());
IEnumerable filters = this.container.Invoke(filterType);
foreach (dynamic actionFilter in filters)
{
actionFilter.OnActionExecuting((dynamic)attribute, context);
}
}
}
public void OnActionExecuted(ActionExecutedContext context)
{
throw new NotImplementedException();
}
}
Мой вопрос: как я могу прочитать атрибуты метода действия в ASP.NET Core MVC?