views:

21

answers:

1

I've tried to write my own HttpModule (IHttpModule) that adds a Header like that:

public class MyModule: IHttpModule
{
    public void Init(HttpApplication c)
    {

        c.BeginRequest += delegate{c.Response.AddHeader("MyHeader", "MyValue");};
    }

    public void Dispose(){}
}

and tried to read in a aspx page like that:

var x = Request.ServerVariables["MyHeader"];

but it didn't work. Any idea why?

+1  A: 

You're adding something to the headers that will be sent from the server to the client and trying to read it from the headers already received by the server from the client. These are two completely different collections.

If you are using this to communicate between the module and the page, you may find it preferable to add something to the HttpContext.Items, this allows for all sorts of objects to be passed, and doesn't pollute the headers with stuff that aren't relevant there, nor require sessions, so it is a good way to communicate between code operating on the same request.

Jon Hanna