System.Net HttpStatusCode class does not have code 422
Asked Answered
M

3

48

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.

Madelinemadella answered 21/4, 2015 at 3:27 Comment(5)
thats what i am trying to ask....if i want to use 422 can i extend this enum?Madelinemadella
No you can not, why do you want extend the ResponseCode enum. That's using standard response code. https://mcmap.net/q/27046/-enum-quot-inheritance-quotCharlenacharlene
Is Respose really a HttpStatusCode?Con
@codesinchaos. Let us assume it is for now. that is not the question.Madelinemadella
@Madelinemadella 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.Con
S
42

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..
}
Sinuate answered 29/9, 2015 at 22:54 Comment(1)
Cool, glad to hear this works. Makes sense, since enum is actually an int anyway.Sensibility
H
6

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
}
Hydrothermal answered 5/6, 2020 at 21:31 Comment(0)
M
1

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.

Mailbox answered 21/6, 2017 at 21:44 Comment(2)
StatusCodes.Status422UnprocessableEntity is part of Microsoft.AspNetCore.Http.Abstractions in case anyone reads this and wonders where it is.Metaphase
@Darrell, nice spot. But for me, that doesn't seem to be enough justification for adding a reference to the additional assembly.Dekameter

© 2022 - 2024 — McMap. All rights reserved.