2012-07-16 118 views
1

我使用VS 2008與IIS6。我想從Http Header「Server」中刪除服務器標籤。 我在Global.asax中使用了下面的代碼。刪除HTTP頭「服務器」。在IIS 6

void Application_PreSendRequestHeaders(object src, EventArgs e) 
{ 
     HttpContext.Current.Response.Headers.Remove("Server"); 
} 

它顯示錯誤「未設置對象實例的對象引用」。 我怎樣才能解決這個

回答

1

這裏是爲我工作(對於IIS6)的步驟:

  1. 下載並在Web服務器上安裝UrlScan 2.5
  2. 打開URLScan配置文件(%WINDIR%\ SYSTEM32 \ INETSRV \ URLScan的\ URLScan.ini中)
  3. 找到將RemoveServerHeader線和的值設置爲1(應爲將RemoveServerHeader = 1)
  4. 復位IIS和測試,如果報頭消失

只是爲了記錄......如果升級到IIS 7(集成管道模式),您可以通過自定義HttpModule代碼實現此目的。

祝你好運!

0

看看這裏的答案:https://stackoverflow.com/a/12804722/2074016。它有額外的錯誤處理,可能會解決您的錯誤:

protected void Application_PreSendRequestHeaders(object sender, EventArgs e) 
{ 
    // Remove the "Server" HTTP Header from response 
    HttpApplication app = sender as HttpApplication; 
    if (null != app && null != app.Request && !app.Request.IsLocal && 
     null != app.Context && null != app.Context.Response) 
    { 
     NameValueCollection headers = app.Context.Response.Headers; 
     if (null != headers) 
     { 
      headers.Remove("Server"); 
     } 
    } 
} 
+0

這需要IIS管道模式,因爲OP問題指出在IIS6中不可用。 – 2013-12-03 15:33:25