0

我有一个看起来像这样的 WebAPI 方法:

    public HttpResponseMessage Login([FromBody] LoginType form)
    {
        if (LoginFails(form.Email, form.Password)
             return Request.CreateErrorResponse(HttpStatusCode.OK, 
                            "Sorry your login failed");

        return this.Request.CreateResponse(HttpStatusCode.OK);
    }

我有一个看起来像这样的客户:

        var sample = new LoginType()
        {
            login = "test",
            Password = "password"
        };

        var client = new HttpClient();
        client.BaseAddress = new Uri("www.example.com);
        client.DefaultRequestHeaders.Accept.Add(
               new MediaTypeWithQualityHeaderValue("application/json"));

        var result = client.PostAsJsonAsync("api/security/login", sample).Result;

问题 1:在结果变量中,我可以看到消息“对不起,您的登录失败”??
问题 2:返回 CreateErrorResponse 时,返回 HttpStatusCode.OK 是否有意义?

4

1 回答 1

1

Q1:从Response.Content。如果内容类型是 application/json,你可以这样做:

response.Content.ReadAsStringAsync().Result;

Q2:HttpStatusCode.Unauthorized如果登录失败我会返回。

于 2013-10-18T20:12:54.167 回答