5

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

4

2 回答 2

6

删除 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。

于 2015-03-04T12:27:43.517 回答
2

我首先想到的是在以下位置拦截BeginRequest事件global.asax

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, "");
    }
}
于 2013-01-03T22:08:50.033 回答