BadRequest 自定义错误消息未返回给客户端?
BadRequest custom error message not returned to client?
我正在开发 Web API 2 应用程序,并且正在实施请求验证。我包含了一个验证检查,如下所示:
if (string.IsNullOrEmpty(userCredentials.UserName))
return BadRequest("UserCredentials.UserName is required");
按预期返回了 400 响应代码,但提供的消息似乎未包含在返回给客户端的响应中。我是不是在实现中遗漏了一些东西,或者我是否需要一种特殊的方式来处理客户端收到的响应?
更新
BadRequest 消息返回给 Postman,但是当我通过控制台应用程序使用 C# 调用它时,我找不到验证消息。这是我在控制台应用程序中使用的代码:
static async Task<User> Authenticate(string domain, string userName, string password)
{
using (var client = GetHttpClient())
{
var encoding = Encoding.GetEncoding("iso-8859-1");
var userName64 = Convert.ToBase64String(encoding.GetBytes(userName));
var password64 = Convert.ToBase64String(encoding.GetBytes(password));
var credentials = new { DomainName = domain, UserName = userName64 /*, Password = password64*/ };
var response = await client.PostAsJsonAsync("api/v1/auth", credentials);
var user = await response.Content.ReadAsAsync<User>();
return user;
//return response.Content.ReadAsAsync<User>();
}
}
您没有检查不良反应。您似乎假设所有响应都是 200,因为您没有检查,只是尝试将响应内容解析为您的 return 类型。
//...
var response = await client.PostAsJsonAsync("api/v1/auth", credentials);
if(response.IsSuccessStatusCode) { // If 200 OK
//parse response body to desired
var user = await response.Content.ReadAsAsync<User>();
return user;
} else {
//Not 200. You could also consider checking for if status code is 400
var message = await response.Content.ReadAsStringAsync();
//Do something with message like
//throw new Exception(message);
}
//...
using(var response = await client.PostAsJsonAsync("api/v1/auth", credentials)){
if(response.IsSuccessStatusCode) {
//This Code is Executed in Case of Success
var user = await response.Content.ReadAsAsync<User>();
return user;
}
else {
//This Code is Executed in Case of In Case of Other Than Success
var message = await response.Content.ReadAsStringAsync();
}
}
You Can use this refactored code, If you want to catch the error message in case of bad request or NotFound etc.
我正在开发 Web API 2 应用程序,并且正在实施请求验证。我包含了一个验证检查,如下所示:
if (string.IsNullOrEmpty(userCredentials.UserName))
return BadRequest("UserCredentials.UserName is required");
按预期返回了 400 响应代码,但提供的消息似乎未包含在返回给客户端的响应中。我是不是在实现中遗漏了一些东西,或者我是否需要一种特殊的方式来处理客户端收到的响应?
更新
BadRequest 消息返回给 Postman,但是当我通过控制台应用程序使用 C# 调用它时,我找不到验证消息。这是我在控制台应用程序中使用的代码:
static async Task<User> Authenticate(string domain, string userName, string password)
{
using (var client = GetHttpClient())
{
var encoding = Encoding.GetEncoding("iso-8859-1");
var userName64 = Convert.ToBase64String(encoding.GetBytes(userName));
var password64 = Convert.ToBase64String(encoding.GetBytes(password));
var credentials = new { DomainName = domain, UserName = userName64 /*, Password = password64*/ };
var response = await client.PostAsJsonAsync("api/v1/auth", credentials);
var user = await response.Content.ReadAsAsync<User>();
return user;
//return response.Content.ReadAsAsync<User>();
}
}
您没有检查不良反应。您似乎假设所有响应都是 200,因为您没有检查,只是尝试将响应内容解析为您的 return 类型。
//...
var response = await client.PostAsJsonAsync("api/v1/auth", credentials);
if(response.IsSuccessStatusCode) { // If 200 OK
//parse response body to desired
var user = await response.Content.ReadAsAsync<User>();
return user;
} else {
//Not 200. You could also consider checking for if status code is 400
var message = await response.Content.ReadAsStringAsync();
//Do something with message like
//throw new Exception(message);
}
//...
using(var response = await client.PostAsJsonAsync("api/v1/auth", credentials)){
if(response.IsSuccessStatusCode) {
//This Code is Executed in Case of Success
var user = await response.Content.ReadAsAsync<User>();
return user;
}
else {
//This Code is Executed in Case of In Case of Other Than Success
var message = await response.Content.ReadAsStringAsync();
}
}
You Can use this refactored code, If you want to catch the error message in case of bad request or NotFound etc.