我有一个看起来像这样的 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 是否有意义?