2

complete novice at C# reporting in

Lets say I have the following code

HttpResponseMessage response = ...

How would I check if the status code for response is a 403?

The property StatusCode is an object - as opposed to an integer, So I am not quite sure what to do.

1

1 Answer 1

10

You can either use the HttpStatusCode enum or cast the enum to an integer:

if (response.StatusCode == HttpStatusCode.Forbidden)
{
   ...
}

or

if ((int)response.StatusCode == 403)
{
   ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

@AlanSTACK I would like to add that the first one is the preferred way: speaking names which self-document their meaning vs magic numbers. What does 403 even mean? Here it means "Forbidden" - so why not write it out then.
Agree 100%! @ckuri
What about the case of a status code that is not part of the enum?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.