2014-03-31 38 views
5

我想在asp mvc 5中使工作自定义错误页面,但由于一些奇怪的原因,目前来测试我的页面,从elmah我登录两个错误(真正的错误是什么我测试和错误页面相关的一个错误未找到:自定义错误页面与ASP.NET MVC 5和Elmah

视图“错误”或它的主人没有被发现或没有视图引擎支持的搜索位置以下地点被搜查: 〜/查看/ HotTowel/Error.aspx 〜/查看/ HotTowel/Error.ascx 〜/查看/共享/ Error.aspx 〜/查看/共享/ Error.ascx 〜/查看/ HotTowel/Error.cshtml 〜/查看/ HotTowel/Error.vbhtml 〜/查看/共享/ Error.cshtml 〜/查看/共享/ Error.vbhtml

我一直在寻找进入这个网址http://doingthedishes.com/2011/09/10/custom-errors-mvc-3-elmah.html,在这里笔者有同样的问题但读它与asp.net的MVC 3后,我试图删除调用HandleErrorAttribute:

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     //filters.Add(new HandleErrorAttribute()); 
    } 
} 

但问题仍然存在:我可以看到我的自定义页面,但asp.net的MVC抛出两个例外。 有什么帮助吗?

解决方案是重写从HandleErrorAttribute派生的类? 想要这个职位:keep getting The view "Error" not found when using Elmah and asp.net mvc 4

回答

8

你可以从ELMAH.MVC 2.0.2 is out如下:

  1. 设置disableHandleErrorFiltertrue

    public class FilterConfig 
    { 
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
        { 
         // filters.Add(new HandleErrorAttribute()); // <-- comment out 
        } 
    } 
    

<add key="elmah.mvc.disableHandleErrorFilter" value="true" /> 
  • FilterConfig类删除

  • 0

    以下是您可能的解决方案。我通常会覆盖基本控制器类中的OnException方法。 filterContext.HttpContext.IsCustomErrorEnabled在web.config中检查<customErrors>showVerboseErrors变量来自web.config中的一个设置。

    protected override void OnException(ExceptionContext filterContext) 
    { 
        if (filterContext.HttpContext.IsCustomErrorEnabled) 
        { 
         //trigger elmah 
         Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception); 
    
         //get the last elmah error 
         var errorList = new List<ErrorLogEntry>(); 
         Elmah.ErrorLog.GetDefault(filterContext.HttpContext.ApplicationInstance.Context).GetErrors(0, 1, errorList); 
         var error = errorList.LastOrDefault(); 
    
         //return the custom error page 
         filterContext.Result = new ViewResult 
         { 
          ViewName = "~/Views/Shared/Error.cshtml", 
          ViewData = new ViewDataDictionary() { 
           { "ErrorDetails", showVerboseErrors && error != null ? filterContext.Exception.Message : null }, 
           { "ErrorId", error != null ? error.Id : null } 
          } 
         }; 
    
         //stop further error processing 
         filterContext.ExceptionHandled = true; 
        } 
        else 
        { 
         base.OnException(filterContext); 
        } 
    } 
    
    相关问题