【问题标题】:global.asax Application_Error event not firingglobal.asax Application_Error 事件未触发
【发布时间】:2015-08-04 08:24:19
【问题描述】:

在我的 web.config 中,我没有类似 CustomErrors 的内容。因为我希望一切都由 Global.asax 处理。

这是我的 web.config:

<?xml version="1.0"?>

<configuration>
  <system.web>
    <compilation targetFramework="4.5" debug="false"/>
    <authentication mode="Forms" >
      <forms loginUrl="login.aspx" name=".ASPNETAUTH" protection="None" path="/" timeout="20" >
      </forms>
    </authentication>

    <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,:,\,?" targetFramework="4.5" maxRequestLength="1048576"/>

    <httpModules>
      <add name="ImageResizingModule" type="ImageResizer.InterceptModule"/>
    </httpModules>

  </system.web>

  <system.webServer>

    <validation validateIntegratedModeConfiguration="false"/>

    <modules>
  <add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule"/>
  <remove name="UrlAuthorization"/>
  <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/>
  <add name="ImageResizingModule" type="ImageResizer.InterceptModule"/>
</modules>
    <security>
      <requestFiltering allowDoubleEscaping="true">

      </requestFiltering>
    </security>

  </system.webServer>

</configuration>

这是 Global.asax 中的 Application_Error 事件:

void Application_Error(object sender, EventArgs e)
{
    try
    {
        Response.Redirect("~/error.html");
    }
    catch
    {
    }
}

我需要 web.config 吗?

【问题讨论】:

    标签: asp.net web-config global-asax imageresizer


    【解决方案1】:

    我建议在您的 web.config 中使用 CustomErrors 部分来完成此操作。

    也许也可以在这里看看:Is global.asax Application_Error event not fired if custom errors are turned on?

    例如,您可以尝试将其添加到您的 web.config。注意,我没有测试它,只是从提到的帖子中复制它:

    <customErrors defaultRedirect="~/error.html" mode="RemoteOnly">
    </customErrors>
    

    实际上有一篇关于该主题的非常好的博客文章以及您拥有的选项: http://www.codeguru.com/csharp/.net/net_debugging/article.php/c19411/Web-Application-Error-Handling-in-ASPNET.htm

    根据我在上述(和相关)文章中收集的内容,我几乎可以猜到,您需要在 Application_Error 中调用 Server.ClearError,否则错误将冒泡到 web.config 错误处理,这是默认情况下从远程查看应用程序时将显示友好的错误消息。 (值为“RemoteOnly”)。

    【讨论】:

    • 如果不亲自尝试,我无法确定,但考虑到我在上面发布的文章,我想说您必须明确关闭 CustomErrors 才能完全控制错误行为。我会试试看。
    • 我刚刚看到一篇关于这个主题的好文章,在上面的帖子中添加了链接。
    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 2011-11-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    相关资源
    最近更新 更多