HOWTO: Use HTTPModule to add items to Response.ServerVariables?

G

Guest

Hello,

I am trying to add items to the ServerVariables Collection for some custom
processing. In the past this needed to be done with an ISAPI filter. I am
told now that I can do this with a class file that implements IHTTPModule.

Below is my code but I can't get it to work. I don't get any errors but I
don't get any headers added to my collection either. Let me note that I have
added the correct data to the web.config file and the Begin process End
process messages do appear on the pages so I know the code and module are
connected. But, I cannot get any items added to ServerVariables collection

******* CODE START **************
namespace HttpModuleExamples
{
public class CustomHttpModule : IHttpModule
{
// IHttpModule members
public void Init(HttpApplication httpApp)
{
httpApp.BeginRequest +=
new EventHandler(this.OnBeginRequest);

httpApp.EndRequest +=
new EventHandler(this.OnEndRequest);
}

public void Dispose()
{
// Usually, nothing has to happen here...
}

// event handlers
public void OnBeginRequest(object o, EventArgs ea)
{
HttpApplication httpApp = (HttpApplication) o;
HttpContext ctx = HttpContext.Current;
ctx.Response.Buffer = true;
ctx.Response.AppendHeader("HTTP-FOO","YES");
ctx.Response.AppendHeader("HTTP_TEST","TRUE");
ctx.Response.AppendHeader("AUTH_TYPE","foo");
ctx.Response.AppendHeader("Special_Header","TEST");
ctx.Response.Write("Beginning Request 2<br>");

// Classic ASP used to give error because of duplicate above, ASP.NET
does nothing
ctx.Response.AddHeader("HTTP-FOO","NO");

// Does not work: Exception Details: System.Web.HttpException: Cannot
directly modify server variables.
//ctx.Request.ServerVariables.Set("HTTP-MYHDR","SomeValue");

// Does not work: Exception Details: System.Web.HttpException: Cannot
directly modify server variables
//ctx.Request.ServerVariables.Add("SM_HDR","SOME DATA");

}

public void OnEndRequest(object o, EventArgs ea)
{
HttpApplication httpApp = (HttpApplication) o;
HttpContext ctx = HttpContext.Current;

ctx.Response.Write("Ending Request <br>");
}
}
}

*********** CODE END ***********

*********** TEST HARNESS ********
<%@ Page Language="C#" trace="false"%>
<html>
<body>
<head>
<br>
Hello World! Here's how the HttpModules work...
<br>
<br>
</head>
<table border=1>
<tr>
<td><b>Variable Name</b></td>
<td><b>Value</b></td>
</tr>
<% foreach (String var in Request.ServerVariables.AllKeys)
{
Response.Write("<tr>");
Response.Write("<td><b>" + var + "</b></td>");
Response.Write("<td>" + Request.ServerVariables[var] + "&nbsp</td>");
Response.Write("</tr>");
}
%>
</table>
</body>
</html>

******** TEST HARNESS END **************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top