posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Http Headers programmatically using the HttpModule

source: http://idunno.org/displayBlog.aspx/2006080101
Here is the original post.

Scott Hanselman wrote today about P3P and adding headers, which got me thinking. For this site (however dead it is) I can control the actual server, so adding the P3P headers was easy. However I have a community site bubbling under and if it takes off it will be need to be moved somewhere professional and not my attic. With professional hosting comes that lack of control and the need to have a better way of adding headers, and Scott made a throw away comment that sparked a day of fun;

If you don't have access to your IIS instance or your ISP doesn't want to help you out, you can also add these HTTP Headers programmatically using an HttpModule.

So I present to you idunno.Web.HttpHeaders. This is a configurable HttpModule, allowing you to use web.config to specify what headers and values you wish added to requests. I will freely admit to taking the shell from Blowery's HTTP Compression module and it works pretty much the same. Add the right lines to your web.config and away you go.

First add the references to the configuration section <configSections>
  <sectionGroup name="idunno.web">
    <section name="httpHeaders"
      type="idunno.Web.HttpHeaders.SectionHandler, idunno.Web.HttpHeaders"/>
  </sectionGroup>
</configSections>

Then add the configuration section

<idunno.web>
  <httpHeaders>
    <headers>
      <add name="X-Test" value="pigeon" />
      <remove name="X-Powered-By" />
    </headers>
    <excludedMimeTypes>
      <add type="image/jpeg"/>
      <add type="image/gif"/>
    </excludedMimeTypes>
    <excludedPaths>
      <add path="NoCustomerHeaders.aspx"/>
    </excludedPaths>
    </httpHeaders>
</idunno.web>

And finally add the module into the pipeline

<system.web>
  ...
  <httpModules>
    <add name="HttpHeadersModule"
      type="idunno.Web.HttpHeaders.HttpModule, idunno.Web.HttpHeaders"/>
  </httpModules>
  ...
</system.web>

Note that if you use the same name as an existing header your value should overwrite the existing value.

Print | posted on Wednesday, August 09, 2006 9:46 PM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 7 and type the answer here:

Powered by: