簡體   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