In my application I need to set a http response header. I'd like to do this in web.config.
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.
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.
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".
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.
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", "*"); } #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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With