从HttpStatusCode生成int值

时间:2016-06-22 08:45:08

标签: c#

这里我想返回如下的整数:

{
    "statusCode": "200"
} 

我没有将statuscode作为整数。

var response = new HttpResponseMessage();
response.StatusCode = Request.CreateResponse((HttpStatusCode)200;

2 个答案:

答案 0 :(得分:1)

Console.Write((int)response.StatusCode);

HttpStatusCode(response.StatusCode的类型)是一个枚举,其中成员的值与HTTP状态代码匹配,例如

public enum HttpStatusCode
{
    ...
    Moved = 301,
    OK = 200,
    Redirect = 302,
    ...
}

答案 1 :(得分:0)

 public int DoHttpOperation(HttpClient client, parameters param)
        {
            url = "url which you want to hit";
            client.DefaultRequestHeaders.Add("headername", "headervalue");
            var response = //operation which you want to perfrom GET/PUT/POST/DELETE
            return ((int)response.StatusCode);  //Here you will o/p 
        }