【发布时间】:2018-07-09 20:00:44
【问题描述】:
我正在开发(至少尝试...)一个连接到 API 的 xamarin android 应用程序。以下是我尝试使用 Post 执行的操作的基本示例。我不确定如何将错误消息(不仅仅是代码)返回给客户端?只要能提供完整的示例,我愿意接受更好的方法。
API 代码:
[HttpPost("Consume")]//*
public IActionResult ConsumeInventory([FromBody] Part part)
{
try
{
_repo.ConsumeInventory(part);
}
catch (Exception ex)
{
//ModelState.AddModelError("Error", ex.Message);
return NotFound("Not Enough Inventory");
}
return Ok(part);
}
客户端调用:
public async Task<HttpResponseMessage> UpdatePartAsync(Part part, string post_url)
{
string jsonString = JsonConvert.SerializeObject(part);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsync("invcount/" + post_url, new StringContent(jsonString, Encoding.UTF8, "application/json"));
if (response.StatusCode == HttpStatusCode.NotFound)
{...//Would like to display "Not enough Inventory" in here}
return response;
}
【问题讨论】: