【发布时间】:2019-06-30 05:36:28
【问题描述】:
所以我刚刚遵循了这个教程: https://code-maze.com/global-error-handling-aspnetcore/ 一切似乎都运行良好,但在服务方面它不处理任何异常,因此它关闭了我的应用程序。在调试方面没什么大不了的,但我需要它在真实环境中工作,并且我想捕获每个异常,这样它就不会关闭应用程序。
StatusService.cs
public async void Add(List<Status> status)
{
throw new Exception();
await this.status.InsertManyAsync(status);
}
StatusController.cs
[HttpPost("add")]
public IActionResult Add( [FromBody] List<Status> status)
{
if(status.Count > 0)
{
try
{
statusService.Add(status);
return Ok();
}
catch (Exception ex) { throw ex; }
}
return BadRequest("Array must not be empty");
}
因此,如果在 StatusController 处引发异常,它可以正常工作,但是当我调用 statusServices 并引发异常时,我的应用程序可以正常工作。 是否有一种解决方法可以让控制器抛出异常,或者让我的中间件在每个类中捕获?
【问题讨论】:
标签: asp.net exception .net-core