开发者

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

In my application I need to set a http respo开发者_JAVA技巧nse 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", "*");

    }

    #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!


You can always add an item to the configuration.appSettings section.

Then your master page, custom base page class, or a specific page can set those http headers by reading from the web.config


I believe none of the answers here are comprehensive, so here's mine:

Please see my blog post on how to add a custom HTTP module to add/remove HTTP headers here. Although the issue I was trying to solve was different but I too needed to add/remove HTTP headers. A gist of the blog post is as follows:

The event to do this in is HttpContext.PreSendRequestHeaders. You can also do this from IIS settings or IIS's config as shown on IIS Knowledge base. This config file (applicationHost.config) is located at %WinDir%\System32\Inetsrv\Config\applicationHost.config for a default installation.


I'm not aware that you can do it on the web.config

So far I know the best options you have are:

  • Create an HTTPModule where you set the header for all responses
  • Add the header on the IIS

Here you have the reference on MSDN:

Custom HttpModule Example

This link has an implementation of an HTTPModule that seems to be what you need

http://idunno.org/archive/2006/08/01/252.aspx


There is no built-in functionality that does this

You can make an HttpModule that reads headers from web.config and adds them to the response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜