Na podstawie this article próbuję utworzyć implementację IActionFilter
dla Core ASP.NET, która może przetwarzać atrybuty zaznaczone na kontrolerze i akcji kontrolera. Chociaż odczyt atrybutów kontrolera jest łatwy, nie mogę znaleźć sposobu na odczytanie atrybutów zdefiniowanych w metodzie akcji.Jak odczytać atrybuty metody działania w ASP.NET Core MVC?
Oto kod mam teraz:
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();
}
}
Moje pytanie brzmi: w jaki sposób mogę odczytać atrybuty metody działania w ASP.NET MVC rdzenia?
Dostaniesz 'MemberInfo' dla jakiejkolwiek metody, którą chcesz użyć interfejsu API do refleksji, następnie użyj' GetCustomAttributes' na tym. Mam nadzieję, że nie pomylę się z pytaniem: –
@Asad: pokaż mi. – Steven