12

Is there a way to get the HTTP status code from MVC action from OnActionExecuted, without using the session variables?

Brian
  • 197
  • 4
  • 14

1 Answers1

7

There are a few ways to access it. Mainly via the ActionExecutedContext which inherits from ControllerContext

protected override void OnActionExecuted(ActionExecutedContext filterContext) {
    var statusCode = filterContext.HttpContext.Response.StatusCode;
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 7
    See also https://stackoverflow.com/questions/50463634/actionfilter-response-statuscode-is-always-200 - "ActionFilter Response.StatusCode is always 200". Need to use `context.Result` instead. – Roland Pihlakas Feb 12 '20 at 05:44