System.Net HttpStatusCode class does not have code 422

Is there a way to handle http status code 422 gracefully. I am looking for the best practice here. I know that HttpStatusCode is an enum so what i tried is this,

HttpStatusCode Unprocessable = (HttpStatusCode)422;
if (Response == (HttpStatusCode)422)

but does not allow me to compare it. Am i doing something wrong here?

Whats the best possible way to add this status code at runtime.


I was using RestSharp which returns the server response status code in a property of type HttStatusCode and I needed to check for a 422 response myself but the of course the type doesn't include it. Fortunately I was still able to test using the following:

if(response.StatusCode == (HttpStatusCode)422)
{
    // Do my stuff..
}

The older versions of .NET don't have this HTTP status code but some of the newer ones do (See MS Docs).
If you are using one of the older frameworks, then you can get the response and check the StatusCode like the following (like Nick said):

var httpResponseCode = Response as HttpWebResponse;
if (httpResponseCode.StatusCode == (HttpStatusCode)422)
{
    //your code here
}

If more than one action in your API can possibly return a 422, you could consider wrapping the check in an extension method like so:

    public static bool IsUnprocessableEntityResponse(this HttpResponseMessage message)
    {
        Requires.NotNull(message, nameof(message));

        return (int) message.StatusCode == StatusCodes.Status422UnprocessableEntity;
    }

Which you can then use in your client like so:

    if (response.IsUnprocessableEntityResponse())
        return Response.UnprocessableEntity<Resource>();

While also avoiding the 422 magic number in your source code at the same time.