MVC 3中的自定义错误无法正常工作

时间:2011-10-25 02:04:28

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

我试图通过在web.config中指定自定义错误来在MVC 3中使用自定义错误页面。

我已经使用一个Action方法 Index()定义了 ErrorController ,该方法用于应用程序中的所有错误

我的 web.config 如下所示:

<configuration>
<configSections>
 <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
  <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
<sectionGroup name="elmah">
  <section name="errorMail" requirePermission="false"  type="Elmah.ErrorMailSectionHandler, Elmah"/>
  <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
  <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
  <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
</sectionGroup>
</configSections>

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
  </namespaces>
</pages>
</system.web.webPages.razor>

<appSettings>
 <add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<httpHandlers>
  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
  <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
</httpHandlers>
<httpModules>
  <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
  <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
</httpModules>
<customErrors defaultRedirect="/Error/Index" mode="On">
  <error statusCode="404" redirect="/Error/Index"/>
</customErrors>
<!--
    Enabling request validation in view pages would cause validation to occur
    after the input has already been processed by the controller. By default
    MVC performs request validation before a controller processes the input.
    To change this behavior apply the ValidateInputAttribute to a
    controller or action.
-->
<pages
    validateRequest="false"
    pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  <controls>
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
  </controls>
</pages>
</system.web>
<system.net>
<mailSettings>
  <smtp deliveryMethod="Network">
    <network host="smtp.gmail.com" port="587" defaultCredentials="false" userName="myUsername" enableSsl="true" password="myPassword"/>
  </smtp>
</mailSettings>
</system.net>
<elmah>
<errorMail   from="from@email.com"  to="to@email.com" subject="WDMS Error" async="true" smtpPort="587" useSsl="true"/>
</elmah>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />

<handlers>
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
</handlers>

<modules runAllManagedModulesForAllRequests="true">
  <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
  <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
  <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</modules>
</system.webServer>
</configuration>

出于某种原因,当我的应用程序发生错误时,它没有点击我的 /错误/索引操作。

这有什么明显的错误吗?有没有办法在global.asax Application_Error()?

中定义一个通用错误消息操作方法

1 个答案:

答案 0 :(得分:1)

您可以使用一种完全不同的机制,通过扩展它将Elmah绑定到标准MVC错误处理属性。这将允许您显示所有应用程序错误的Error.cshtml视图。

How to get ELMAH to work with ASP.NET MVC [HandleError] attribute?