Is it possible to skip the whole action method execution and return a specific ActionResult
when a certain condition is met in OnActionExecuting
?
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.
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.
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.
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); }
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