开发者

replace asp.net Response.Output with my own content

开发者 https://www.devze.com 2023-01-01 15:29 出处:网络
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.

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号