假设我有以下变量:
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
}
这些替代方法都不能说服我,我希望可以使用.NET类或方法为我完成这项工作,例如:
bool isSuccess = HttpUtilities.IsSuccess(status);
int code = (int)Response.StatusCode
,从那里,你将需要创建自己Enum
这里检查工作示例stackoverflow.com/questions/1330856/...