Caching Output HTML

S

Smithers

Just wondering what it would take to cache a copy of the output HTML from a
dynamically constructed aspx page before it is sent to the browser.

Reason being: the page is constructed of a few user controls, each of which
queries a SQL Server database for their content. The content is not likely
to change frequently at all - so I'd like a way to cache the final page upon
first request. Then subsequent requests are served from the cache - with no
need to do the expensive database queries on subsequent requests for the
cached page. Logic that updates the database would clear the cache so that
no stale data would exist in the cache.

1. How can I cache a copy of the page for future use?

2. How can I serve a subsequent request for the page directly from the
cache, rather than running the aspx logic?

Thanks.
 
D

Dave Bush

Use the cache directive in the "<%@Page .." header at the top of the aspx
page?

Something I'm missing or are you just not familiar with this feature of asp.net?

Dave Bush
http://blog.dmbcllc.com
 
S

Smithers

Use the cache directive in the "<%@Page .." header at the top of the aspx
page?

Something I'm missing or are you just not familiar with this feature of
asp.net?

I am familiar with the cache directive. Perhaps I should have included a
couple of additional facts in my OP:

Most of the pages in the site never exist on disk. Instead, I plan to build
them programmatically from scratch, via HttpModule that builds and returns
the "page" to the user. I don't understand how ASP.NET implements caching
via the cache directive when serving up plain old aspx files - so I'm not
clear on how I can emulate that behavior in my HttpModule logic that builds
and returns these dynamic pages.

Until now the 1.1 system I have been working with inserts ascx user controls
into PlaceHolder controls in an aspx page that served as a template. But now
as we migrate to 3.5, I'm wanting to intercept the aspx requests as they
come in from the browser and do *everything* programmatically - including
(1) URL rewriting, (2) page construction, (3) retrieve the HTML of the final
page, (4) cache that HTML, (5) return the HTML to the requesting browser.
Subsequent requests for the same page would return the HTML from the cache.

Step 3 is what I am unclear on. How do I get the rendered HTML of a
dynamically constructed aspx before it is sent to the browser?

Thoughts? Considerations?

Thanks.
 
M

Masudur

I am familiar with the cache directive. Perhaps I should have included a
couple of additional facts in my OP:

Most of the pages in the site never exist on disk. Instead, I plan to build
them programmatically from scratch, via HttpModule that builds and returns
the "page" to the user. I don't understand how ASP.NET implements caching
via the cache directive when serving up plain old aspx files - so I'm not
clear on how I can emulate that behavior in my HttpModule logic that builds
and returns these dynamic pages.

Until now the 1.1 system I have been working with inserts ascx user controls
into PlaceHolder controls in an aspx page that served as a template. But now
as we migrate to 3.5, I'm wanting to intercept the aspx requests as they
come in from the browser and do *everything* programmatically - including
(1) URL rewriting, (2) page construction, (3) retrieve the HTML of the final
page, (4) cache that HTML, (5) return the HTML to the requesting browser.
Subsequent requests for the same page would return the HTML from the cache.

Step 3 is what I am unclear on. How do I get the rendered HTML of a
dynamically constructed aspx before it is sent to the browser?

Thoughts? Considerations?

Thanks.

Hi... Here is few Code exampls

protected void Page_Load(object sender, EventArgs e)
{
if (Session["PageOutPut"] != null)
{
if (Session["Counter"] == null)
{
Session["Counter"] = 0;
}
Session["Counter"] = (int)Session["Counter"] + 1;
string PageResult = (string)Session["PageOutPut"];
Response.Write(PageResult + (int)Session["Counter"]);
Response.End();
}
}

protected override void Render(HtmlTextWriter writer)
{
string PageResult = "";
if (Session["PageOutPut"] == null)
{
//get the html output and save it in session
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter hWriter = new HtmlTextWriter(sw);
base.Render(hWriter);
PageResult = sb.ToString();
Session["PageOutPut"] = PageResult;
Response.Redirect(Page.Request.RawUrl);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top