Reputation: 111
I have custom action filter DemoActionFilter , but i don't need to apply this filter for every action one by one because if the controller has lot of actions then it will be a messy.
How can we add controller level but ignore the controller level from the action level. Ex: Higher level Authorize will be ignored with AllowAnonymous
[Route("api/sample")]
[ApiController]
[TypeFilter(typeof(DemoActionFilter))]
public class SampleController : BaseController
{
[HttpGet("one")]
public object Sample()
{
}
//I don't need to apply controller level action filter for this action
[HttpGet("two")]
public object Sample2()
{}
}
public class DemoActionFilter: IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
//DO what ever
}
}
Upvotes: 0
Views: 443
Reputation: 27962
As far as I know, there is no build-in method which could ignore the controller filter like AllowAnonymous attribute.
If you want to ignore the Sample2 in the controller filter. You could try to create if condition in the OnActionExecuting method like below:
public void OnActionExecuting(ActionExecutingContext context)
{
var actionName = ((ControllerBase)context.Controller)
.ControllerContext.ActionDescriptor.ActionName; ;
if (actionName != "two")
{
//... do what you want
}
}
Upvotes: 1