Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop continuation of ASP.NET MVC ActionFilter

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?

like image 833
Jey Geethan Avatar asked Mar 18 '10 09:03

Jey Geethan


People also ask

Is it possible to cancel filter execution?

You can cancel filter execution in the OnActionExecuting and OnResultExecuting methods by setting the Result property to a non-null value.

What is ActionFilter in MVC?

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.

What is difference between middleware and filters in. net Core?

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.


1 Answers

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.

like image 142
Levi Avatar answered Oct 13 '22 02:10

Levi