Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect ActionResult type in ActionFilter?

I have a global ActionFilter executing on each ActionResult. Is it possible to detect the ActionResult type (such as a JsonResult vs. an ActionResult) inside the ActionFilter?

For instance, I want the ActionFilter to immediately exit here:

public JsonResult someAjaxAction()
{
}

But I'd like it to do it's magic here:

public ActionResult PageView()
{
}

Yes, I know I could just use an ActionFilter attribute and decorate the actions I'd like to execute this on, but I'd like to keep it as a global ActionFilter and detect inside the action whether I need to perform the desired work.

like image 877
MaseBase Avatar asked Dec 03 '25 02:12

MaseBase


1 Answers

public class MyActionFilterAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (filterContext.Result is ViewResult)
        {
            ...
        }
        else if (filterContext.Result is JsonResult)
        {
            ...
        }
        ...
    }
}
like image 151
Darin Dimitrov Avatar answered Dec 05 '25 15:12

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!