.net core 3.0 app.UseExceptionHandler 没有在错误方法中遇到断点

.net core 3.0 app.UseExceptionHandler is not hitting break point in Error method

我正在编写 .net core 3.0 web api 并尝试设置全局错误处理。根据文档,这应该是一项简单的任务,但我无法让它工作。当我抛出异常时,我应该将我的错误方法重定向到永远不会遇到我在其中设置的断点。你能告诉我我做错了什么以及如何在我的错误方法中设置断点吗?

这是我的代码:

// startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    app.UseExceptionHandler("/System/Error");
    app.UseStatusCodePages();
    app.UseStatusCodePagesWithReExecute("/system/error/{0}");

}

// SystemController.Error method

public IActionResult Error(
    [Bind(Prefix = "id")] int statusCode = 0
            )
{
    // break point never hits here and I want it to
    var exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
    if (exceptionFeature != null)
    {
        // Get which route the exception occurred at   
        string routeWhereExceptionOccurred = exceptionFeature.Path;
        // Get the exception that occurred   
        Exception exceptionThatOccurred = exceptionFeature.Error;
        // TODO: Do something with the exception   
        // Log it with Serilog?   
    }
        return View();
}

找到问题和解决方案。

问题:最初我会在调试模式下从我的 web api 项目中的 vs 中按 f5 到 运行。在显示的 Web 浏览器的 url 中,我会将控制器方法的参数和 post 放入我的控制器。这个例程导致调试器在我抛出错误的地方中断,而不会在我在全局错误方法中的断点处中断。

解决方案:我使用外部测试工具(如 insomnia 或 postman 来测试网络 API 调用,使其正常工作。我在 Visual Studio 中按 f5 到 运行 处于调试模式的 Web api 项目,然后转移到失眠并 posted 到控制器方法,它在全局错误中崩溃了在我的断点处的方法。因此,您必须使用外部客户端来触发您的控制器,以便在您的全局错误方法中触发断点。