Modifying page output using Session data?

  • Thread starter Thread starter Al Cohen
  • Start date Start date
A

Al Cohen

I'm attempting to build a templating engine that will substitute the
values of session variables for tags in a page before the page gets sent
to the client. For example, the contents of Session["UserName"] would
replace each instance of ${UserName} in the page just before the page is
returned.

In principle, this should be easy to do by hooking into one of the
HttpApplication events that occurs after the request handler runs, but
I'm having trouble figuring out exactly how. The problem is that to do
this, I need to satisfy two conditions:
1. I must have access to the Session
2. I must be able to manipulate (i.e., random search, delete, and write)
the page contents.

It looks like I can only manipulate the page contents in a filter
attached to HttpResponse.Response; otherwise, I can only append to the
Response. (Is this true?)

If I can only manipulate the page in a filter, that would be OK, as long
as the filter has access to the Session. However, a little
experimenting indicates that the Session is gone after
PostRequestHandlerExecute occurs, which is before the response filters
are executed.

So, I seem to be unable to manipulate the entire page and read a session
at the same time.

Any suggestions would be VERY appreciated!

Thanks,

Al Cohen
www.alcohen.com
 
So, I seem to be unable to manipulate the entire page and read a session
at the same time.

Use the OnBeginRequest method, you will need to convert the byte stream to a
string, change your response and then restore it to a byte stream. You can
access the session context from within this method.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
One solution is to override the Render method and pass a html-writer
based on a memory stream to the base.Render method. Then you only have
to read the memory stream and replace whatever you want, no problems
with accessing the session either.

protected override void Render(HtmlTextWriter writer)
{
MemoryStream mem=new MemoryStream();
StreamWriter w=new StreamWriter(mem);
HtmlTextWriter htmlw=new HtmlTextWriter(w);

base.Render (htmlw);
htmlw.Flush();

mem.Position=0;
TextReader reader=new StreamReader(mem);
string output=reader.ReadToEnd();


writer.Write(output.Replace("${user}",(string)Session["username"]));
}

Cheers
Hugo
 
Hugo:

One million thanks!

Not only is this short and sweet, but you already did all of the work
for me!

Best regards,

Al Cohen

Hugo said:
One solution is to override the Render method and pass a html-writer
based on a memory stream to the base.Render method. Then you only have
to read the memory stream and replace whatever you want, no problems
with accessing the session either.

protected override void Render(HtmlTextWriter writer)
{
MemoryStream mem=new MemoryStream();
StreamWriter w=new StreamWriter(mem);
HtmlTextWriter htmlw=new HtmlTextWriter(w);

base.Render (htmlw);
htmlw.Flush();

mem.Position=0;
TextReader reader=new StreamReader(mem);
string output=reader.ReadToEnd();


writer.Write(output.Replace("${user}",(string)Session["username"]));
}

Cheers
Hugo

I'm attempting to build a templating engine that will substitute the
values of session variables for tags in a page before the page gets sent
to the client. For example, the contents of Session["UserName"] would
replace each instance of ${UserName} in the page just before the page is
returned.

In principle, this should be easy to do by hooking into one of the
HttpApplication events that occurs after the request handler runs, but
I'm having trouble figuring out exactly how. The problem is that to do
this, I need to satisfy two conditions:
1. I must have access to the Session
2. I must be able to manipulate (i.e., random search, delete, and write)
the page contents.

It looks like I can only manipulate the page contents in a filter
attached to HttpResponse.Response; otherwise, I can only append to the
Response. (Is this true?)

If I can only manipulate the page in a filter, that would be OK, as long
as the filter has access to the Session. However, a little
experimenting indicates that the Session is gone after
PostRequestHandlerExecute occurs, which is before the response filters
are executed.

So, I seem to be unable to manipulate the entire page and read a session
at the same time.

Any suggestions would be VERY appreciated!

Thanks,

Al Cohen
www.alcohen.com
 
Use the OnBeginRequest method, you will need to convert the byte stream to a
string, change your response and then restore it to a byte stream. You can
access the session context from within this method.

This was sort of my question that no one answered a few days ago about
modifying content on the fly.

You are saying it's possible to catch the Response stream without using a filter
and modify it and then write it back ?

PL.
 
My answer was telling Al how to do this within a filter. Have a look at
Hugo's suggestion also.

Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
My answer was telling Al how to do this within a filter. Have a look at
Hugo's suggestion also.

Thank you for you clarification. I cannot use Hugo's suggestion since it's on a page by
page basis, I need it to work from inside a HttpModule.

PL.
 
Back
Top