Search code examples
c#.net-coreasp.net-mvc-filters

Read status of contex.Result() in override method of OnActionExecuting


I'm overriding OnActionExecuting method in order to perform some checks before calling some controllers' methods and I was wondering if there was any way to read context.Result() value to check if it's equal to - for example - Status 401 Unauthorized or similar, in order to perform different actions on different status, like this...

public override void OnActionExecuting(ActionExecutingContext context) {
    base.OnActionExecuting(context);
    if (/*context.Result()==401*/)
    {
        //do things
    }
    else
    {
        //do other things
    }
}

Is there any way to read context.Result() value/status in order to use it inside an if() statement?


Solution

  • You can get and compare with Response.StatusCode:

    if(context.HttpContext.Response.StatusCode == (int)System.Net.HttpStatusCode.Unauthorized)
    {
        //Rest of code goes here
    }