I want to make sure that the number that the person provides is a legit HttpStatusCode .
At first I thought about using Enum.TryParse(..)or Enum.Parse(..), but it is possible that I am getting the wrong result with the bad data provided.
eg.
In: Enum.Parse(typeof (HttpStatusCode), "1")
Out: 1
In: Enum.Parse(typeof (HttpStatusCode), "400")
Out: BadRequest
In: Enum.Parse(typeof (HttpStatusCode), "aaa")
Out: System.ArgumentException: Requested value 'aaa' was not found.
Ok, so if I go over to the bad aaa values, I get a System.Argument exception. But when I go to number 1 (as text, not int), I get a return value of 1. I expected this to fail and throw an exception.
Passing a value of 400 returns the correct enumeration BadRequest.
Any ideas people?
source
share