本文链接: https://blog.csdn.net/brlite/article/details/115603766
系列文章:
文章目录
Web Api 的错误返回
在使用.net 的 Web Api构建Endpoint对外提供访问时,往往需要统一的错误返回格式。
如果按照前面两篇文章(一)、(二)所说,采用带有ErrorCode的异常ErrorCodeException,那么在WebApi中返回错误时,只要返回ErrorCode即可。
代码如下:
[HttpPut]
[Authorize]
public IActionResult Update(UpdatetNickNameRequest request)
{
try
{
long userId = User.GetUserId();
_identityService.SetNickName(userId, request.NickName);
return Ok();
}
catch (IdentityException ex)
{
//做相应处理
//... ...
return new BadRequestObjectResult(ex.ErrorCode);
}
catch