阻止 Asp.net MVC WebAPI 在捕获到异常时停止(需要按继续)

Prevent Asp.net MVC WebAPI from stopping (needing to press continue) when exception is caught

我的 asp.net MVC webAPI 项目的控制器中有一个 try and catch 块。

如果我弄乱了日期输入,这是我得到的异常

An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code

Additional information: An item with the same key has already been added.

我尝试添加以下内容

      catch (Exception e)
        {
            output.Add("requestStatus", "fail");
            output.Add("errorSumary", "=== Exception in SOAP request ===");
            output.Add("errorSuggestion", "effectiveDate was not in the right format or the date already past");
            logger.Error("Error: ActivateController - effectiveDate was not in the right format");
        }

但是当触发此异常时,我仍然必须按 visual studio 中的继续按钮才能使该程序保持 运行。 有什么方法可以使异常不停止程序的 运行 吗? (也就是说,我不必单击 visual studio 中的绿色继续按钮)

我知道我应该尝试捕获每种类型的异常并优雅地处理它们我只想要一个内置的故障安全装置,如果有人在地毯下滑倒,它允许它保持 运行(然后我记录然后使用 Nlog 所以你可以通过 splunk 之类的东西查看是否有任何问题。

所以事实证明,为了解决这个问题,我所要做的就是更改 visual studio 中的以下设置,以便在 运行 处于调试模式时进行检查。

设置调用

break when this exception type is user-unhandled .

我能够通过从 "debug to Release" 切换 Visual Studio 发布配置来确认这一点,在该配置中,程序不仅会捕获异常,而且程序将不再停止执行,从而继续捕获它一次又一次地不中断程序。我没有意识到默认情况下的调试配置已经打开了开箱即用的 "break when this exception type is user-unhandled"。

你可以在我找到的两个地方看到你的例外设置。 调试 -> 异常(如果您想重新启用此行为,同样将它们设置回默认值)。

可以在此处找到有关调试设置的更多信息 工具->选项->调试