ASP .Net MVC中的自定义错误

时间:2016-03-10 17:14:29

标签: c# asp.net asp.net-mvc asp.net-mvc-4 custom-error-pages

我尝试在ASP .Net MVC Application中设置自定义错误处理。在Web.config中我有这个:

  <system.web>
    <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/page500.aspx">
      <error statusCode="404" redirect="~/Error/page404.aspx"/>
      <error statusCode="500" redirect="~/Error/page500.aspx"/>
    </customErrors>

<system.webServer>
  <handlers>
    <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
  </handlers>
  <httpErrors errorMode="Custom" existingResponse="Replace">
    <clear/>
    <error statusCode="404" responseMode="File" path="/Error/page404.html"/>
    <error statusCode="500" responseMode="File" path="/Error/page500.html"/>
  </httpErrors>
</system.webServer>

当我尝试网址时:

http://localhost:49376/private/aaa

我可以看到我的404自定义错误页面

enter image description here

当我尝试网址时

http://localhost:49376/private/aaa<

我还可以看到我的500自定义错误页面

enter image description here

但是当尝试网址时:

http://localhost:49376/private/aaa/foo/bar

我收到了IIS默认错误页面。当我尝试

http://localhost:49376/private/aaa<script></script> 

我收到了运行时默认错误页面。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我不确定,但它可能是你的elmah配置。在这里阅读更多相关信息,特别是在评论部分:http://www.troyhunt.com/2012/01/aspnet-session-hijacking-with-google.html?m=1

如果您将配置设置有点错误,我认为这是一个已知的Elmah问题

长话短说,请在配置中尝试:

<location path="elmah.axd">
  <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/page500.aspx">
  <error statusCode="404" redirect="~/Error/page404.aspx"/>
  <error statusCode="500" redirect="~/Error/page500.aspx"/>
</customErrors>
  <system.web>
    <httpHandlers>
      <add verb="POST,GET,HEAD" path="elmah.axd" 
        type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>
    <authorization>
      <allow roles="Admin" />
      <deny users="*" />
    </authorization>
  </system.web>
  <system.webServer>
    <handlers>
      <add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD"
        type="Elmah.ErrorLogPageFactory, Elmah"
        preCondition="integratedMode" />
    </handlers>
    <httpErrors errorMode="Custom" existingResponse="Replace">
    <clear/>
      <error statusCode="404" responseMode="File" path="/Error/page404.html"/>
      <error statusCode="500" responseMode="File" path="/Error/page500.html"/>
    </httpErrors>
  </system.webServer>
</location>