System.Net HttpStatusCode class does not have code 422

20,502

Solution 1

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..
}

Solution 2

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
}

Solution 3

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.

Share:
20,502

Related videos on Youtube

golldy
Author by

golldy

Updated on July 09, 2022

Comments

  • golldy
    golldy almost 2 years

    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.

    • Peyman
      Peyman about 9 years
      Just check this link, there is no 422 status code
    • golldy
      golldy about 9 years
      thats what i am trying to ask....if i want to use 422 can i extend this enum?
    • Peyman
      Peyman about 9 years
      No you can not, why do you want extend the ResponseCode enum. That's using standard response code. stackoverflow.com/questions/757684/enum-inheritance
    • CodesInChaos
      CodesInChaos about 9 years
      Is Respose really a HttpStatusCode?
    • golldy
      golldy about 9 years
      @codesinchaos. Let us assume it is for now. that is not the question.
    • CodesInChaos
      CodesInChaos about 9 years
      @golldy In that case I'll need to vote-to-close this as "not reproducible". Please post a working example that exhibits the problem. (HttpStatusCode)422 works perfectly well for me, at least when using the full framework.
  • Matt
    Matt almost 8 years
    Cool, glad to hear this works. Makes sense, since enum is actually an int anyway.
  • Darrell
    Darrell almost 5 years
    StatusCodes.Status422UnprocessableEntity is part of Microsoft.AspNetCore.Http.Abstractions in case anyone reads this and wonders where it is.
  • Chris W
    Chris W over 4 years
    @Darrell, nice spot. But for me, that doesn't seem to be enough justification for adding a reference to the additional assembly.