views:

40

answers:

2

I am trying to intercept a asp.net web request and based on a lookup replace the page thats going to be rendered to the client.

example: If a request for "/about-us" comes to my web server, i will first see if i have a version of this in the database, otherwise i will revert to flat files. The version I will retrieve from the database will be a .aspx page that has to be rendered and contain web controls and inline serverside script.

What is the best way to go about this?

I have tried overriding the CreateHtmlTextWriter method but this seems to be too late in the process as the TextWriter passed to this method is already instantiated.

I have also tried to Implement my own PageHandlerFactory but this seems to create an instance of Page of which I cannot seem to override its internal setting of the Response.Output stream.

Am i barking up the wrong tree here? what is the best approach to take here?

+3  A: 

Implement your own HttpHandler - this can intercept the calls, you can pick stuff up from DB or from the file system and send those as a response. You simply need to implement IHttpHandler in your class and configure IIS to use it.

If all you are going to do is output text (in the form of an ASPX page), then it would not go through the IIS aspx engine. To do that, you would have to dynamically load, compile and execute such a page - a very difficult thing to do. This Microsoft KB article can be of help.

Oded
how would i go about rendering aspx controls and server side tags?I've done this approach but ended up writing out the source code of my .aspx, it was not rendered
WebDude
@WebDude - If all you are going to do is output text (in the form of an ASPX page), then it would not go through the IIS aspx engine. To do that, you would have to dynamically load, compile and execute such a page - a very difficult thing to do.
Oded
and ironically exactly what i'm trying to do :(
WebDude
@WebDude - perhaps this Microsoft KB article can be of help. http://support.microsoft.com/kb/910441
Oded
That's great, please can you update your original answer with a reference to that link, It's exactly what I was looking for. Thanks!
WebDude
@WebDude - Answer updated.
Oded
A: 

The problem you're going to come against is that simply retrieving an aspx page and rendering it won't work properly, and you can't dynamically load an ASPX page, you have to use Server.Transfer or Response.Redirect.

You could do this via user controls and Page.LoadControl using a virtual path provider to get the control from the database, but I think this will be complicated and over-engineered.

If you've got the time to look into it and have the framework support, I'd definitely look at using ASP.Net MVC for this.

Russ C
You could also do Response.Redirect to a http handler that downloads the aspx page, which will then render in the ASP.Net life-cycle properly, but this isn't a very nice way to do things and will be a nightmare to debug. IMO.
Russ C