keep getting The view “Error” not found when using

2019-04-16 14:06发布

问题:

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