Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip action execution from an ActionFilter?

Is it possible to skip the whole action method execution and return a specific ActionResult when a certain condition is met in OnActionExecuting?

like image 604
user49126 Avatar asked Mar 23 '12 09:03

user49126


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 MVC action filters?

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.

Which action filter triggers first?

Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter. Authorization filters are used to implement authentication and authorization for controller actions.

What is action filter in c#?

Action filters are used to implement the logic that get executed before or after a controller action executes. Authorization Filters. It is used to implement authorization and authentication for action filters. Result Filters. Result filters contains logic that gets executed before or after a view result gets executed.


1 Answers

You can use filterContext.Result for this. It should look like this:

public override void OnActionExecuting(ActionExecutingContext filterContext) {     //Check your condition here     if (true)     {         //Create your result         filterContext.Result = new EmptyResult();     }     else         base.OnActionExecuting(filterContext); } 
like image 173
tpeczek Avatar answered Sep 18 '22 19:09

tpeczek