【问题标题】:How can I get requests to trace.axd to return 404?如何获得对 trace.axd 的请求以返回 404?
【发布时间】:2012-12-31 14:15:59
【问题描述】:

在我的生产环境中,我想将所有请求重定向到 /trace.axd 以返回 HTTP 404。目前,默认的 HTTP 500 正在返回。这会在我们的分析工具中产生各种不必要的噪音。环境是 IIS 7.5 上的 ASP.NET 4.0 Web 表单。

【问题讨论】:

    标签: asp.net iis webforms iis-7.5


    【解决方案1】:

    删除 Web.config 文件中的跟踪 HTTP 处理程序:

      <system.webServer>
        <!-- remove TraceHandler-Integrated - Remove the tracing handlers so that navigating to /trace.axd gives us a 
             404 Not Found instead of 500 Internal Server Error. -->
        <handlers>
          <remove name="TraceHandler-Integrated" />
          <remove name="TraceHandler-Integrated-4.0" />
        </handlers>
      </system.webServer>
    

    现在导航到 /trace.axd 会给我们一个 404 Not Found 而不是 500 Internal Server Error。

    【讨论】:

      【解决方案2】:

      首先想到的是在global.asax中拦截BeginRequest事件:

      protected void Application_BeginRequest()
      {
          // assuming that in your production environment debugging is off
          if (!HttpContext.Current.IsDebuggingEnabled && Request.RawUrl.Contains("trace.axd"))
          {
              HttpContext.Current.Response.StatusCode = 404;
              HttpContext.Current.Response.End();
              // or alternatively throw HttpException like this:
              // throw new HttpException(404, "");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多