假设我有以下变量:
System.Net.HttpStatusCode status = System.Net.HttpStatusCode.OK;
如何检查这是一个成功状态代码还是一个失败状态代码?
例如,我可以做以下事情:
int code = (int)status;
if(code >= 200 && code < 300) {
//Success
}
我也可以有一些白名单:
HttpStatusCode[] successStatus = new HttpStatusCode[] {
HttpStatusCode.OK,
HttpStatusCode.Created,
HttpStatusCode.Accepted,
HttpStatusCode.NonAuthoritativeInformation,
HttpStatusCode.NoContent,
HttpStatusCode.ResetContent,
HttpStatusCode.PartialContent
};
if(successStatus.Contains(status)) //LINQ
{
//Success
}
None of these alternatives convinces me, and I was hoping for a .NET class or method that can do this work for me, such as:
bool isSuccess = HttpUtilities.IsSuccess(status);