Is there a way to get the HTTP status code from MVC action from OnActionExecuted
, without using the session variables?
Asked
Active
Viewed 4,074 times
12

Brian
- 197
- 4
- 14
-
what are you trying to achieve ? – Shyju Feb 24 '17 at 17:27
-
Is it necessary use `OnActionExecuted`? Can be used `OnResultExecuted`? – Roman Feb 24 '17 at 17:34
-
I needed to get status of response to be able to log it in the API logging. – Brian Feb 25 '17 at 08:22
1 Answers
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
-
7See 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