keep getting The view “Error” not found when using Elmah and asp.net mvc 4

匆匆过客 提交于 2019-12-04 20:14:46

We created an empty MVC5 app and added ELMAH to it. We also were receiving the extra error you described even though we did not add the HandleErrorAttribute. After some research I found the nuget package Elmah.MVC which adds some additional configuration settings. In the appSettings section of web.config you will find these 2 lines:

<appSettings>
    <add key="elmah.mvc.disableHandler" value="false" />
    <add key="elmah.mvc.disableHandleErrorFilter" value="false" />
</appSettings>

These 2 keys default to "false". I changed their values to "true" and the extra logged exception went away.

I am developing an application using ASP.NET MVC 5 RC and I use Elmah too for error logging. I am using too a custom error handling attribute to redirect errors to a custom action on a custom controller, but mine doesn't look like the one shown in the link you provided.

However I had the same problem: Elmah was properly logging the error, but was also adding a "Error view not found" entry. I solved this by adding the following line to the OnException method on the attribute:

filterContext.ExceptionHandled = true;

For completeness, this is the complete code for the custom error handling attribute I am using:

public class CustomHandleErrorAttribute: HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        filterContext.ExceptionHandled = true;

        if(filterContext.HttpContext.Request.IsAjaxRequest()) {
            filterContext.HttpContext.Response.StatusCode = 
                (int)HttpStatusCode.InternalServerError;
            filterContext.Result = new ContentResult() {
                Content = "Server error",
                ContentType = "text/plain"
            };
        }
        else {
            filterContext.Result = new RedirectToRouteResult(
                "Default",
                new System.Web.Routing.RouteValueDictionary(new
                {
                    controller = "Error",
                    action = "ApplicationError"
                }));
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!