Tutorial :Is it possible to add response http headers in web.config?


In my application I need to set a http response header. I'd like to do this in web.config.


The best way to do this would be the <customHeaders> element of the web.config file. Note that this only works for IIS version 7 and above.

The configuration to add your example header would be:

<configuration>    <system.webServer>      <httpProtocol>        <customHeaders>          <add name="Content-Language" value="*" />        </customHeaders>      </httpProtocol>    </system.webServer>  </configuration>  

For more information see the IIS "Custom Headers" Configuration Reference page


Solution Finally, after a long search I found the solution. Create a class with this code:

public class myHTTPHeaderModule : IHttpModule  {        #region IHttpModule Members        public void Dispose()      {        }        public void Init(HttpApplication context)      {          context.EndRequest += new EventHandler(context_EndRequest);      }        void context_EndRequest(object sender, EventArgs e)      {          HttpResponse response = HttpContext.Current.Response;            response.AddHeader("Content-Language", "*");        }        

Next Post »