You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
usingMicrosoft.AspNetCore.Mvc;namespaceFiltersDemo;publicclassExampleFilterAttribute:TypeFilterAttribute{publicExampleFilterAttribute(AnEnumenumValue):base(typeof(ExampleFilter)){// ...// Put the arguments into an object list like this:Arguments=newobject[]{enumValue};}}
Update the constructor on the Filter to accept the arguments
usingSystem;
...namespace FiltersDemo;publicclassExampleFilter:IAsyncAuthorizationFilter{privatereadonlyILogger_logger;privatereadonlyAnEnum_enumValue;publicExampleFilter(AnEnumenumValue,ILogger<ExampleFilter>logger){_logger=logger??thrownewArgumentNullException(nameof(logger));// ILogger<T> would be coming down from DI container_enumValue=enumValue;// enumValue comes down from the Arguments of the attribute above.}publicasyncTaskOnAuthorizationAsync(AuthorizationFilterContextcontext){
...}}
Apply the attribute with the argument
usingSystem;
...namespace FiltersDemo;// Apply the filter, with an argumnet of EnumValue.Query.[ExampleFilter(EnumValue.Query)]publicclassMyController:ControllerBase{
...}