Cache Pages With Server.Execute

Request a page, then store the returned text in a TextWriter object.

Dino Esposito

October 30, 2009

2 Min Read
ITPro Today logo in a gray background | ITPro Today

asp:Feature

TECHNOLOGIES:Server.Execute | TextWriter | Caching

LANGUAGES:VB .NET

 

Cache Pages With Server.Execute

Request a page, then store the returned text in aTextWriter object.

 

By Dino Esposito

 

The Server.Execute method executes a request to anotherpage and lets you decide how to use the returned text. By default, the outputis appended to the response stream of the caller page. By using an overload ofExecute, however, you can cache all the generated output in a TextWriterobject, such as System.IO.StringWriter:

 

Dim writer As StringWriter = New StringWriter()

Server.Execute("childpage.aspx", writer)

 

This behavior of the Execute method is like calling alanguage function and storing its return value in some local variable forfurther processing. The output of the child page is in no way required to beHTML. For instance, it instead can be XML. In this case, by combining theExecute method with the XML server control, you easily can set up amultibrowser presentation layer. You put this tag in the body of the page:

 

 

Finally, bind the XML server control dynamically to theXML contents generated by Execute and the XSLT file that is correct for thecurrent browser:

 

Sub Page_Load()  

Dim writer As StringWriter = New StringWriter()

Server.Execute("childpage.aspx", writer)

presLayer.DocumentContent = writer.ToString()

presLayer.TransformSource = GetXsltForBrowser()

End Sub

 

This can really come in handy if you're trying to reuse asmuch of your old ASP-based applications as possible.

 

For more, see Dino's article PassValues Between Web Pages.

 

Dino Esposito is a trainer and consultant whospecializes in ASP.NET, ADO.NET, and XML. A member of Wintellect and co-founderof http://www.VB2TheMax.com,Dino wrote BuildingWeb Solutions with ASP.NET and ADO.NET and the upcoming Programming ASP.NET,both from Microsoft Press. Write to him at mailto:[email protected].

 

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like