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

后端 未结 6 955
挽巷
挽巷 2020-12-29 02:35

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

相关标签:
6条回答
  • 2020-12-29 03:20

    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

    0 讨论(0)
  • 2020-12-29 03:20

    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

    0 讨论(0)
  • 2020-12-29 03:22

    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

    0 讨论(0)
  • 2020-12-29 03:26

    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!

    0 讨论(0)
  • 2020-12-29 03:36

    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 讨论(0)
  • 2020-12-29 03:39

    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.

    0 讨论(0)
提交回复
热议问题