且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

捕获ASP.NET MVC + Web Api中的所有异常

更新时间:2023-02-15 22:46:12

将其放在您的Global文件中:

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();

    // Do something with the error.
    System.Diagnostics.Debug.WriteLine(exception);

    // Redirect somewhere or return an error code in case of web api
    Response.Redirect("/Home/Error");
}

任何时候,都会发生错误,ASP MVC会调用该方法.

Anytime, an error occurs, ASP MVC will call that method.