找不到错误页面,使用自定义错误页面引发ELMAH错误

我对Global.asax做了一些修改,以便我可以显示自定义错误页面(403,404和500)这是代码:

public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } protected void Application_Error(object sender, EventArgs e) { if (Context.IsCustomErrorEnabled) { ShowCustomErrorPage(Server.GetLastError()); } } private void ShowCustomErrorPage(Exception exception) { HttpException httpException = exception as HttpException; if (httpException == null) { httpException = new HttpException(500, "Internal Server Error", exception); } Response.Clear(); RouteData routeData = new RouteData(); routeData.Values.Add("controller", "Error"); routeData.Values.Add("fromAppErrorEvent", true); switch (httpException.GetHttpCode()) { case 403: routeData.Values.Add("action", "AccessDenied"); break; case 404: routeData.Values.Add("action", "NotFound"); break; case 500: routeData.Values.Add("action", "ServerError"); break; default: routeData.Values.Add("action", "DefaultError"); routeData.Values.Add("httpStatusCode", httpException.GetHttpCode()); break; } Server.ClearError(); IController controller = new ErrorController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); } } 

我还在Web上添加了以下内容.Config:

    

自定义错误页面正确显示,ELMAH将正确记录(故意)抛出的错误。 但ELMAH还会捕获并记录一个额外的错误:

 System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/account/Error.aspx ~/Views/account/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/account/Error.cshtml ~/Views/account/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml 

我的第一直觉导致我在filter配置中禁用了全局HandleErrorAttribute 。 并且,类似的SO问题,例如: 自定义错误页面的MVC问题让我相信我的怀疑是正确的。 但即使在禁用全局HandleErrorAttribute我仍然得到错误,无法找到错误视图! 是什么赋予了? 我唯一的另一个预感是我的基本控制器派生自System.Web.Mvc.Controller我试图检查源,看看HandleErrorAttribute是否应用于System.Web.Mvc.Controller但无法收集任何东西……

更新:我试图覆盖我的基本控制器来标记exception,如下所示:

 protected override void OnException(ExceptionContext filterContext) { filterContext.ExceptionHandled = true; base.OnException(filterContext); } 

但这并没有解决问题。

UPDATE2:我将一个Error.aspx文件放入共享视图中,只是为了看看会发生什么。 当它在那里时,ELMAH记录强制exception,然后提供共享视图 – 它永远不会到达Application_Error() ….不太确定该怎么做。

终于让它让我满意……

Elmah.Mvc包应用“隐藏”error handling程序。 我已通过在web.config 添加以下行来禁用此function(默认情况下,该值从nuget install设置为“false”)

  

所以,现在我的错误传播到Application_Error并由Elmah记录,绕过Elmahfilter,并显示正确的错误页面(不是/shared/error.cshtml中的那个)

如果您在IIS 7集成模式下运行,则需要添加Response.TrySkipIisCustomErrors = true; 在Application_Error中。 否则,IIS仍会将客户端重定向到自定义错误页面,尽管您在代码中执行了任何操作。

有关其他详细信息,请参见此处: http : //www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors

编辑:这是我的Application_Error的正文:

 if (HttpContext.Current != null) { Server.ClearError(); Response.TrySkipIisCustomErrors = true; RouteData data = new RouteData(); data.Values.Add("controller", "Error"); data.Values.Add("action", "Error"); IController controller = new MyApp.Controllers.ErrorController(); controller.Execute(new RequestContext(new HttpContextWrapper(Context), data)); }