How to remove ASP.Net MVC Default HTTP Headers?

前端 未结 11 2191
自闭症患者
自闭症患者 2020-11-28 01:23

Each page in an MVC application I\'m working with sets these HTTP headers in responses:

X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version         


        
11条回答
  •  孤城傲影
    2020-11-28 01:49

    X-Powered-By is a custom header in IIS. Since IIS 7, you can remove it by adding the following to your web.config:

    
      
        
          
        
      
    
    

    This header can also be modified to your needs, for more information refer to http://www.iis.net/ConfigReference/system.webServer/httpProtocol/customHeaders


    Add this to web.config to get rid of the X-AspNet-Version header:

    
      
    
    

    Finally, to remove X-AspNetMvc-Version, edit Global.asax.cs and add the following in the Application_Start event:

    protected void Application_Start()
    {
        MvcHandler.DisableMvcResponseHeader = true;
    }
    

    You can also modify headers at runtime via the Application_PreSendRequestHeaders event in Global.asax.cs. This is useful if your header values are dynamic:

    protected void Application_PreSendRequestHeaders(object source, EventArgs e)
    {
          Response.Headers.Remove("foo");
          Response.Headers.Add("bar", "quux");
    }
    

提交回复
热议问题