Asp.net Web Api set response status code to number
You can cast any int to a HttpStatusCode.
response.StatusCode = (HttpStatusCode)422;
You can also:
HttpResponseMessage response = Request.CreateResponse((HttpStatusCode)422, "Unprocessable Entity");
I ended up creating a class for this:
public class HttpStatusCodeAdditions
{
public const int UnprocessableEntityCode = 422;
public static HttpStatusCodeAdditions UnprocessableEntity = new HttpStatusCodeAdditions(UnprocessableEntityCode);
private HttpStatusCodeAdditions(int code)
{
Code = code;
}
public int Code { get; private set; }
public static implicit operator HttpStatusCode(HttpStatusCodeAdditions addition)
{
return (HttpStatusCode)addition.Code;
}
}
which can be used like this:
response.StatusCode = HttpStatusCodeAdditions.UnprocessableEntity;