asp.net-mvc - 捕获ASP.NET MVC错误和WebActivator

标签 asp.net-mvc error-handling webactivator

我想知道是否有可能在不修改web.config或global.asax文件的情况下捕获未处理的错误。

理想情况下,我将使用WebActivator包并具有以下内容:

[assembly: WebActivator.PostApplicationStartMethod(typeof(MyProject.Initialize), "Start")]

namespace MyProject
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mvc;

    internal static Initialize
    {
        public static void Start()
        {
              // this doesn't work
              HttpContext.Current.ApplicationInstance.Error += ApplicationInstance_Error;
        }

        // this never fires
        static void ApplicationInstance_Error(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}

问题#1:是否可以在不修改web.config或global.asax文件的情况下捕获未处理的错误?

问题2:如何?

更新:我在下面发布了答案。我不确定这是否是最好的方法。任何意见表示赞赏。

最佳答案

使用这种方法,我可以使某些东西正常工作:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Initialize), "Start")]

namespace MyProject
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Mvc;

    internal static Initialize
    {
        public static void Start()
        {
            GlobalFilters.Filters.Add(new WatchExceptionAttribute());
        }
    }

    public class CustomHandleErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            // do your thing here.
        }
    }
}

关于asp.net-mvc - 捕获ASP.NET MVC错误和WebActivator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10184072/

相关文章:

c# - 如何在 Web Api Controller 中触发 OnActionExecuting?

node.js - Node 连接超时 : how to suppress "ServiceUnavailableError: Response timeout" log message

validation - Sequelize 验证 - 是否可以按属性/字段进行 Sequelize 组错误?

c# - NinjectMVC3 的 WebActivator.PreApplicationStartMethod 程序集属性在我的 View 源代码编辑器中导致警告

asp.net-mvc-3 - 在 Application_Start 中设置和运行代码的首选方式

jquery - 如何将字典作为参数从 jQuery/Ajax 传递给 ActionResult 方法?

ASP.NET MVC : Action Filter to set up controller variables?

java - 为什么我运行这个程序时没有弹出一个框架?约格尔

asp.net-mvc-3 - 为 MVC3 应用程序配置 Ninject 的正确方法是什么?

asp.net-mvc - 用于多个应用程序的 mvc 4 web api