首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >捕获ASP.NET MVC错误和WebActivator

捕获ASP.NET MVC错误和WebActivator
EN

Stack Overflow用户
提问于 2012-04-17 01:54:36
回答 1查看 906关注 0票数 1

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

理想情况下,我应该使用WebActivator包并拥有如下内容:

代码语言:javascript
复制
[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:如何?

更新:我在下面发布了一个答案。我不确定这是不是最好的方法。如有任何意见请见谅。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-17 02:24:43

我用这种方法可以得到一些有用的东西:

代码语言:javascript
复制
[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.
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10184072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档