我想知道是否有可能在不修改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/