在 ASP.Net MVC 中删除不必要的HTTP响应头


本教程基于 IIS8

移除项

移除X-AspNet-Version HTTP头

  <system.web>

    <!--移除X-AspNet-Version HTTP头-->
    <httpRuntime enableVersionHeader="false" /> 

  </system.web>

移除X-AspNetMvc-Version HTTP头

//Global.asax.cs
protected void Application_Start()
{
    //移除X-AspNetMvc-Version HTTP头
    MvcHandler.DisableMvcResponseHeader = true;
}

移除或修改 X-Powered-By HTTP 头

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <!--MVC routes.MapRoute加后缀(.html)显示404-->
    <modules runAllManagedModulesForAllRequests="true"/>
      <!--To remove X-Powered-By, in the web.config find/create <system.webServer> and add-->
      <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
        <add name="X-Powered-By" value="LAdmin Dev" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

移除或修改 Server HTTP 头

protected void Application_BeginRequest(object sender, EventArgs e)
{
    //To remove Server
    var application = sender as HttpApplication;
    if (application != null && application.Context != null)
    {
        //application.Context.Response.Headers.Remove("Server");
            application.Context.Response.Headers.Set("Server", "LAdmin Server");
    }
}

更多

You can use the PreSendRequestHeaders and PreSendRequestContext events with native IIS modules, but do not use them with managed modules that implement IHttpModule. Setting these properties can cause issues with asynchronous requests. The correct version is to use BeginRequest event.

HttpRuntime的Web.config里的配置

<httpRuntime
   executionTimeout = "number" 
   maxRequestLength = "number" 
   requestLengthDiskThreshold = "number" 
   useFullyQualifiedRedirectUrl = "[True|False]" 
   minFreeThreads = "number" 
   minLocalRequestFreeThreads = "number" 
   appRequestQueueLimit = "number"
   enableKernelOutputCache = "[True|False]" 
   enableVersionHeader = "[True|False]" 
   apartmentThreading = "[True|False]"
   requireRootedSaveAsPath = "[True|False]"
   enable = "[True|False]" 
   sendCacheControlHeader = "[True|False]" 
   shutdownTimeout = "number"
   delayNotificationTimeout = "number"
   waitChangeNotification = "number" 
   maxWaitChangeNotification = "number" 
   enableHeaderChecking = "[True|False]" 
/>

知识共享许可协议
《在 ASP.Net MVC 中删除不必要的HTTP响应头》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告