【问题标题】:MVC 3, Elmah and The view 'Error' or its master was not foundMVC 3、Elmah 和未找到视图“错误”或其主人
【发布时间】:2013-04-01 20:24:41
【问题描述】:

我正在开发一个使用 MVC 3、elmah 和 nhibernate 的旧网站。 Elmah 日志实际上有数以千计的“未找到视图‘错误’或其主人”错误。我认为它掩盖了真正的错误。我不知道如何让 Elmah 记录真正的错误。

作为尝试调试的一种方式,我添加了 - return RedirectToAction("noWhere"); - 强制出错。在本地,我得到一个 .net 屏幕,它只是说“处理您的请求时发生异常......”在登台时我得到 YOSOD 屏幕,告诉我设置 web.config customerrors 节点。两者都将 customerrors 设置为 on。

网络配置有以下内容:

 <pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>
<httpModules>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
  <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
  <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>


  <customErrors mode="On" defaultRedirect="~/Views/Shared/PageNotFound">
      <error statusCode="404" redirect="~/Views/Shared/PageNotFound" />
      <error statusCode="500" redirect="~/Views/Shared/PageNotFound" />
  </customErrors>

Global.asax 有:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
         filters.Add(new ElmahHandleErrorAttribute());
    }

Elmah 类有

public class ElmahHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute 
{
    public override void OnException(ExceptionContext context)         
    {             
        base.OnException(context);               
        var e = context.Exception;             
        if (!context.ExceptionHandled // if unhandled, will be logged anyhow      
            || RaiseErrorSignal(e)    // prefer signaling, if possible 
            || IsFiltered(context))   // filtered?
            return;               
        LogException(e);         
    }  

baseController 类有:

protected ViewResult PageNotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        return View("PageNotFound", PageViewModelBuilder.UpdateSiteProperties(new PageViewModel()));
    }

    protected ViewResult PageBadRequest()
    {
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return View("PageNotFound", PageViewModelBuilder.UpdateSiteProperties(new PageViewModel()));
    }

任何帮助记录正确的错误将不胜感激......

【问题讨论】:

    标签: c# asp.net-mvc-3 elmah


    【解决方案1】:

    在您的 web.config 中将 "~/Views/Shared/PageNotFound" 更改为 "~/Views/Shared/PageNotFound.aspx”(或 "~/Views/Shared/PageNotFound.chtml”),并确保您的 Shared 文件夹中有 PageNotFound.aspx

    【讨论】:

    • 是的,那就改成这样吧。我已经编辑了我的答案。它解决了你的问题吗?如果有,请标记为答案。
    • 谢谢 - 但是添加 cshtml 会导致抛出 httpexception 并且不会影响原始问题
    • 你现在得到的 httpexception 是什么?我想如果你解决了错误,elmah 只会记录相关的真实错误。
    • System.Web.HttpException at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute - statusCode="404"
    猜你喜欢
    • 2012-04-29
    • 2013-01-17
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2011-01-17
    相关资源
    最近更新 更多