Rolando
Rolando

Reputation: 762

Custom Error Pages with ASP.NET MVC 5 and Elmah

I am trying make work custom error page in asp mvc 5 but for some strange reason at moment to test my page, from elmah i am loging two errors ( the real error what i am testing and a error related with error page not found:

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

I was looking into this url http://doingthedishes.com/2011/09/10/custom-errors-mvc-3-elmah.html, where the author had the same issue but with asp.net mvc 3. After read it, I tried remove the call to HandleErrorAttribute:

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

But the issue is still there: i can see my custom page but asp.net mvc is throwing two exceptions. Any help?

the solution is rewrite a class derived from HandleErrorAttribute ? like this post: keep getting The view "Error" not found when using Elmah and asp.net mvc 4 ?

Upvotes: 7

Views: 5935

Answers (2)

Rolando
Rolando

Reputation: 762

You can do the following from ELMAH.MVC 2.0.2 is out:

  1. Set disableHandleErrorFilter to true:

    <add key="elmah.mvc.disableHandleErrorFilter" value="true" />
    
  2. Remove filters.Add(new HandleErrorAttribute()); from FilterConfig class:

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

Upvotes: 9

prison-mike
prison-mike

Reputation: 480

Here's a possible solution for you. I typically override the OnException method in a base controller class. filterContext.HttpContext.IsCustomErrorEnabled checks <customErrors> in the web.config. The showVerboseErrors variable is derived from a setting in the 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);
    }
}

Upvotes: 1

Related Questions