views:

361

answers:

2

A little background: I am trying to create a lightweight cookieless database-backed user session using a highly striped down ASP.NET implementation. This site audience will be mobile users connecting via celluar networks, so the page sizes need to be very small. I am not using the .NET session, viewstate, etc. and most page contain very few if any server controls.

I want to be able to process the output of a page request so I can modify internal links in the response with my own session information. I have read that there was an ISAPI filter to allow cookieless sessions pre-ASP.NET. That is basically what I want to build, just inside the application.

Has anyone done anything like this? I'm already inheiriting the System.Web.UI.Page class for my page base for other reasons. It seems like I should be able to do something from here.

Thanks

+1  A: 

Look into using an HttpModule for this. You can process the entire response on the way out.

You might also be able to do something with a base class - perhaps go through all the server-side controls that might have links in them on the PreRenderComplete event. This wouldn't help you with HTML <A> tags, though.

John Saunders
+1  A: 

HttpModules can give you complete control over your output, but there are also a couple other things you can do that are a little simpler.

  1. Create a custom Filter for the Response.Filter. More or less you create a Stream that you run everything through before sending it onto the underlying stream letting you make your changes there.

  2. Override the render event for the Page and write all your contents to a string and then make your changes there... for example...

.

//this is from memory, you might need to check it
override void Render(HtmlTextWriter writer) {

StringWriter html = new StringWriter();
HtmlTextWriter render = new HtmlTextWriter(html);
base.Render(render);
string output = html.ToString()

//make your changes to output
//output = ???

writer.Write(output);

}
Hugoware
Okay, I take it you mean the Response.Filter. This seems to be what I was looking for. Many thanks!
Paul
Thanks, you're right - I'll fix it
Hugoware