I have two custom ActionFilters on an action.
In first of the actionfilters, I have an redirect performed if a condition is not met (classic authorization). And in another I have an redirect performed if another condition is not met (say role checking).
But I do not want to continue to the second actionFilter if the first one is not met. How to do this?
You can cancel filter execution in the OnActionExecuting and OnResultExecuting methods by setting the Result property to a non-null value.
ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.
Middleware can be used for the entire request pipeline but Filters is only used within the Routing Middleware where we have an MVC pipeline so Middleware operates at the level of ASP.NET Core but Filters executes only when requests come to the MVC pipeline.
Setting the filterContext.Result
property to any non-null value will stop execution of later filters. So if your first filter sets filterContext.Result = new RedirectResult(...)
, the second filter and action method will never be run. This is how the built-in [Authorization]
filter works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With