Question

I have 2 action filter:

[Auth]
[Check]
public ActionResult Home()
{
//...
}

I need set CheckAttr first work and break AuthAttr.

I do not remember how to do it. Please help me.

example:

request to action Home -> first work Auth filter and logic I need redirect to another action

Was it helpful?

Solution

You could short-circuit the entire execution by setting the Result property of the filterContext:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    filterContext.Result = new JsonResult
    {
        Data = "hello",
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };
}

In this example I have returned a JsonResult but you could return any ActionResult you like: ViewResult, PartialViewResult, RedirectToRouteResult, ...

Once you assign the Result property no more filters will be executed for this context. The action execution itself will be short-circuited as well.

Also bear in mind that if your action filters are deriving from the same type there are no guarantees in which order they will be executed unless you manually assign the Order property on each of them. Of course if they are of different types such as IAuthorizationFilter or IActionFilter, it is obvious that authorization filters will run before any action filters. That's by design.

If on the other hand you are only attempting to prevent other action filters from executing but the action itself should execute, I am afraid that there's no way to achieve that and you will have to rethink your design of those filters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top