繁体   English   中英

.Net core 3.1 如何捕获所有错误并在辅助类中找到异常位置

[英].Net core 3.1 how can catch all errors and find the exception place in the helper classes

  1. 我正在使用app.UseExceptionHandler()中间件来捕获异常,但是有些错误,例如 ex: new Uri(" empty str ") does not throw the error to the exception controller directly 请找到 screen1 和 2?发生错误时的屏幕 1

    按继续后异常将抛出异常 controller按继续后的屏幕 2 为什么异常没有 go 直接到异常 controller?

  2. 以及如何在不使用(尝试并捕获)的情况下捕获辅助类中发生的异常或任何 class 不继承 Controller class 的异常?因为我需要一种动态方法来捕获所有项目文件中的任何异常

您可以在基础 controller 中添加带有 function 回调的方法作为参数。

例如:基础 controller:

 public class BaseUIController
 {
  protected async Task<IActionResult> HandleExceptionCall(Func<Task<IActionResult>> call)
    {
        IActionResult result;
        try
        {
            //Your actual function call back pass here
            result = await call();
        }
        catch (Exception ex)
        {
           //Here, you can handle all the exception.
           result = View("Error");
        }
        return result;
    }
 }

例如:您的孩子 controller:

public class EmployeeController : BaseUIController
{        
    public async Task<IActionResult> GetEmployees()
    {
        return await HandleExceptionCall(async () =>
        {
           //Your api call here
           //Now, If any exception raised, you can handle in your base controller function.
        });
    }
}

“IActionResult 结果;”是什么意思? 在这里做吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM