In Asp.net Web Api, how do I set the status code of my response using an int or string, not the StatusCode enum?
In my case, I\'d like to return validation errors with
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;