exception-handling - 如何处理 ASP.Net MVC 2 中的全局异常

标签 exception-handling asp.net-mvc-2

我的基本 Controller 有这个覆盖:

protected override void OnException(ExceptionContext filterContext)
{
    // http://forums.asp.net/t/1318736.aspx
    if (filterContext == null)
    {
        throw new ArgumentNullException("filterContext");
    }
    // If custom errors are disabled, we need to let the normal ASP.NET exception handler
    // execute so that the user can see useful debugging information.
    if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
    {
         return;
    }

    Exception exception = filterContext.Exception;

    // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
    // ignore it.
    if (new HttpException(null, exception).GetHttpCode() != 500)
    {
        return;
    }
}

我想捕获异常并将其邮寄。我可以做邮件部分,但不确定如何收集异常?我是 MVC 的新手。在 Web 表单中,我在 global.asax 中有这个
void Application_Error(object sender, EventArgs e) 
{
    string AdminEmail = "myEmail@domain";
    // Code that runs when an unhandled error occurs
    Exception objErr = Server.GetLastError().GetBaseException();
    string err = "Error Caught in Application_Error event\n" +
            "Error in: " + Request.Url.ToString() +
            "\n Error Message:" + objErr.Message.ToString() +
            "\n Stack Trace:" + objErr.StackTrace.ToString();
    // Below uses: using System.Diagnostics;
    //EventLog.WriteEntry("Sample_WebApp", err, EventLogEntryType.Error);
    //Server.ClearError(); // Clear error prohibits it from showing on page
    //additional actions...

    MyApp.Utility.Email.Send(AdminEmail, CommonLibrary.FromEmail, "Asp.net Application Error", err);
}

网页配置
<customErrors mode="On" />

最佳答案

我认为您需要在基本 Controller 的覆盖方法中实现相同的逻辑。像这样的东西:

    protected override void OnException(ExceptionContext filterContext)
    {
        string AdminEmail = "myEmail@domain";
        // Code that runs when an unhandled error occurs
        Exception objErr = filterContext.Exception;
        string err = "Error Caught in Application_Error event\n" +
                "Error in: " + Request.Url.ToString() +
                "\n Error Message:" + objErr.Message.ToString() +
                "\n Stack Trace:" + objErr.StackTrace.ToString();
        // Below uses: using System.Diagnostics;
        //EventLog.WriteEntry("Sample_WebApp", err, EventLogEntryType.Error);
        //Server.ClearError(); // Clear error prohibits it from showing on page
        //additional actions...

        MyApp.Utility.Email.Send(AdminEmail, CommonLibrary.FromEmail, "Asp.net Application Error", err);
        base.OnException(filterContext);
    }

关于exception-handling - 如何处理 ASP.Net MVC 2 中的全局异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2730293/

相关文章:

Java 断言,与单元测试和异常相比/对比

silverlight - Plupload 文件浏览器打不开

c# - 为什么 Ajax.BeginForm 在 Chrome 中不起作用?

asp.net-mvc-2 - Azure MVC2 应用程序的表单例份验证提供程序?

c# - 将图像发布到 asp.net mvc Controller 操作

java - 如何通过重新抛出异常来测试代码?

java - JSP异常隐式对象

c++ - 覆盖默认的未处理异常行为

silverlight - Silverlight MVVM异常处理

asp.net-mvc - 从区域调用部分 View