I'm just attempting to send a 404 response message back from an API call however running into some problems.
I have the following function which returns an ActionResult:
private static ActionResult StatusResult(HttpStatusCode statusCode, string reason) => new ContentResult
{
StatusCode = (int)statusCode,
Content = $"Status Code: {(int)statusCode} - {statusCode} | {reason}",
ContentType = "application/json",
};
and call it like so:
return StatusResult(HttpStatusCode.NotFound, "ID not found.");
It returns back to client, however the StatusCode is still 200, not 404. Is there a simple way to fix this?
Response Example:
This may not work for everyone but in the end I edited the HttpContext object directly and returned that back to the client.
Which gave the correct Status Codes as a response.
context.Response.ContentType = "application / json";
context.Response.StatusCode = (int)statuscode;