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

自闭症网瘾萝莉.ら 提交于 2019-12-06 13:45:38

问题


I am using Elmah 1.2 as the logging framework for my asp.net mvc 4 application.

in the web.config file, I set customErrors mode to on.

<customErrors mode="On" defaultRedirect="/Error">
  <error statusCode="404" redirect="/Error/NotFound" />
</customErrors>

I also created a custom HandleErrorAttribute, copied the code from this link.

http://joel.net/logging-errors-with-elmah-in-asp.net-mvc-3--part-4--handleerrorattribute

In my Home controller, i just throw an exception to test the logging framework.

    public ActionResult About()
    {
        throw new Exception("this is a buggggggggggggg");

        ViewBag.Message = "Your app description page.";

        return View();
    }

"this is a buggggggggggggg" is logged in the database, great, it works. then there's another error also logged, and I didnt expect that to happen.

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

Update:

follow Tim's suggestion, then it causes another issue.

If I create a Error.cshtml in the shared folder. when unhandled exception happens, it will show this Error.cshtml file, not "/Error" page. I have customErrors enabled. They should all get redirected to "/Error" page.


回答1:


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.




回答2:


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"
                }));
        }
    }
}


来源:https://stackoverflow.com/questions/18616992/keep-getting-the-view-error-not-found-when-using-elmah-and-asp-net-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!