ActionFilter Response.StatusCode is always 200
You can attempt to convert your ActionExecutedContext.Result
object to ObjectResult
and retrieve StatusCode
from it.
public void OnActionExecuted(ActionExecutedContext context)
{
var statusCode = (context.Result as ObjectResult)?.StatusCode
}
You are looking at the status code of the response before response is actually generated. OnActionExecuting
is called before the action is executed, so no status code is set yet. Default value for status code is 200, and that's what you see.
To be able to see the actual status code other actions have assigned to the response, you need to look at OnActionExecuted
, which runs after the action.
Update.
Another issue might be the fact that in Core framework action filter runs before and after the action is executed. So the response is not processed yet, and status code is not set in the http response object.
The proper method for your use case seems to be IResultFilter.OnResultExecuted