Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

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

like image 564
Martijn Avatar asked May 27 '10 14:05

Martijn


People also ask

How do I add HTTP response headers?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

Can response have headers?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.

Can we modify response header?

Creating an outbound rule to modify the HTTP response header In the main URL Rewrite feature view page click "Add Rules..." and then select "Blank Rule" under the "Outbound Rules" category. In the "Edit Outbound Rule" page name the rule as "Rewrite Location Header".

Can you add custom HTTP headers?

Another example of using a custom HTTP header would be to implement the X-Pull header. You can use this custom header for a variety of purposes including rate limiting bandwidth on your origin server, restricting CDN traffic, creating custom logic on your origin server, etc.


2 Answers

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

like image 160
Adrian Clark Avatar answered Sep 23 '22 05:09

Adrian Clark


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", "*");      }      #endregion } 

(Don't ask me why to use this event, but it works..)

Now add a line in web.config in the HttpModule section:

    <httpModules>         <add type="namespace.myHTTPHeaderModule, assembly name" name="headers" />         <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>     </httpModules> 

And that's it!

like image 23
Martijn Avatar answered Sep 23 '22 05:09

Martijn