无法在Web Api 2中的BadRequest上收到错误消息 [英] Can't get error message on BadRequest in Web Api 2
问题描述
我在Google上搜索了很多问题,没有运气,所以请尝试其他人是否可以帮助我.
I've googled a lot searching for an answer to my problem with no luck so, let's try if someone else can help me.
在进行一些验证之后,我有一个Web Api 2操作来注册用户.如果一切正常,它将返回序列化的用户,否则,将返回BadRequest响应中发送的错误消息.
I have a Web Api 2 action to register an user after doing some validations. If everything works, it return the serialized user and if don't, an error message sent within a BadRequest response.
另一方面,我有一个WPF客户端,它调用API进行响应.
At the other hand, I have a WPF client that calls the API for a response.
我的问题是我无法获取错误发送的原因,客户端仅收到错误请求错误消息.
My problem is that I can't get the reason sent by the error, the client just get the Bad Request error message.
这是代码的一部分:
-
Web Api操作:
Web Api Action:
public async Task<IHttpActionResult> AddUser(NewUserDTO {
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (!model.IdCliente.HasValue)
{
ModelState.AddModelError("", "No existe el cliente");
return BadRequest(ModelState);
}
// do save stuff
return Ok(TheModelFactory.Create(user));
}
客户端功能:
Client function:
public async Task<ApplicationUserDTO> AddUser(NewUserDTO dto) {
using (var client = new HttpClient()) {
client.BaseAddress = new Uri(_urlBase);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token);
HttpContent content = new StringContent(JsonConvert.SerializeObject(dto));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage responseMessage = await client.PostAsync("api/accounts/AddUser", content);
if (responseMessage.IsSuccessStatusCode) {
var responseJson = await responseMessage.Content.ReadAsStringAsync();
user = JsonConvert.DeserializeObject<ApplicationUserDTO>(responseJson);
}
else
MessageBox.Show(responseMessage.Content.ReadAsStringAsync().Result);
return user;
}
}
任何人都可以帮忙吗?
-
DTO:
DTO:
[Serializable]
public class NewUserDTO {
public int? IdCliente { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Email { get; set; }
public string Role { get; set; }
public string Password { get; set; } }
无论如何... dto已正确发送到api螺母,这是您要求的序列化dto:
Anyway... the dto is sent correctly to api nut, here's the serialized dto as you asked for:
"{\"IdCliente\":null,\"UserName\":\"Toni\",\"Email\":\"soft@moreno-csa.com\",\"Role\":\"Filtra\",\"Password\":\"TONI\"}"
推荐答案
最后,我放弃了. 也许在web.config,mvc config,api config处的一些代码,过滤或任何与控制器相比干扰和发送不同响应的代码. 我在DTO中添加了状态代码和消息属性,并始终返回一个对象. 无论如何,感谢所有试图帮助我的人.
Finally, I gived up. Maybe some code at web.config, mvc config, api config, filtering or whatever is disturbing and sending a diferent response than the controller does. I added a status code and a message property to my DTO and return always an object. Thanks anyway to all who tried to help me.
这篇关于无法在Web Api 2中的BadRequest上收到错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!