Many of my friends may have discussed how to generate HTML in ASP. NET. I/O methods are used to write files according to templates.
HTML is generated on the 404 error page, and the page is saved in the render and output to the HTML file.
Today, I am sending a method to use httphander to manually compile the. ASPX page with URL rewriting.
HTML files can be generated on demand and on a regular basis to reduce database access.
Statement: The text below is an indispensable part of this article. repost it. Thank you!
//////////////////////////////////////// ////////////
Author: Wu meibo <active target. Net>
Also launched:
Logged-on & blog
Developer Institute &. Net boys community
Knowledge points: Writing urlrewrite, ihttpmodule, and ihttphander
Effect:
Http://www.devedu.com/Doc/DotNet/AspNet/default.2.aspx
Http://www.devedu.com/Doc/DotNet/AspNet/default.2.html
Ideas:
1. Mount the request for ". aspx" to the custom httphander.
2. Configure URL rewriting rules
3. When accessing an. aspx file, check whether the file should be generated according to the configuration in httphander.
Then...
If (to be generated)
{
If (if an HTML file has been generated)
{
If (the file has not expired)
{
Then direct (server. Transfer ()).
}
Else
{
Delete HTML files;
Re-compile. aspx (database operations in the page, etc)
Generate HTML files;
}
}
Else if (File not generated yet)
{
Generate HTML.
}
}
Else
{
Compile the. aspx file.
}
Also: Read the discussion about Asp.net page compilation in Dudu's blog.
Http://www.cnblogs.com/dudu/archive/2006/03/07/345107.html
Http://www.cnblogs.com/dudu/archive/2006/03/07/344351.html
Some code:
[Copy to clipboard]
Code:
Public void processrequest (httpcontext context)
{
String rawurl = context. Request. rawurl;
String requestpath = context. Request. path;
String applicationpath = context. Request. applicationpath;
URL urlitem = NULL;
// Tostaticurlitem is not defined in the context. This request is directly compiled without urlrewrite and does not generate html
// Refer to urlrewritemodule. CS
If (context. items ["tostaticurlitem"] = NULL)
{
If (! File. exists (context. Request. physicalpath ))
{
Throw new httpexception (404, "the page you accessed is not found. ");
}
// Asp.net 1.1 uses the following method to compile the page
// Pageparser. getcompiledpageinstance (requestpath, context. Request. physicalpath, context). processrequest (context );
Ihttphandler hander = buildmanager. createinstancefromvirtualpath (requestpath, typeof (page) as ihttphandler;
Hander. processrequest (context );
Return;
}
String filepath;
Urlitem = (URL) Context. items ["tostaticurlitem"];
RegEx = new RegEx (
Globals. applicationpath + urlitem. lookfor,
Regexoptions. cultureinvariant | regexoptions. singleline | regexoptions. Compiled | regexoptions. ignorecase );
String requestfile = RegEx. Replace (rawurl, globals. applicationpath + urlitem. writeto. Replace ("^ ","&"));
If (requestfile. indexof ("? ")> 0)
{
Filepath = requestfile. substring (0, requestfile. indexof ("? "));
}
Else
{
Filepath = requestfile;
}
String inputfile = context. Request. physicalapplicationpath + filepath;
String Path = context. Request. physicalapplicationpath + rawurl. tolower (). Replace (". aspx", ". html ");
If (applicationpath! = "/")
{
Inputfile = inputfile. Replace (applicationpath + "/",@"\");
Path = path. Replace (applicationpath + "/", ""). Replace ("/",@"\");
}
Else
{
Path = path. Replace ("/",@"\");
}
If (! Urlitem. enabledtostatic)
{
// Asp.net 1.1 uses the following method to compile the page
// Pageparser. getcompiledpageinstance (filepath, inputfile, context). processrequest (context );
Ihttphandler hander = buildmanager. createinstancefromvirtualpath (filepath, typeof (page) as ihttphandler;
Hander. processrequest (context );
Return;
}
If (! File. exists (PATH ))
{
// Asp.net 1.1 uses the following method to compile the page
// Pageparser. getcompiledpageinstance (filepath, inputfile, context). processrequest (context );
Ihttphandler hander = buildmanager. createinstancefromvirtualpath (filepath, typeof (page) as ihttphandler;
Hander. processrequest (context );
Context. response. Filter = new aspxboy. buildhtmldemo. tohtmlfilter (context. response. filter, PATH );
Return;
}
If (urlitem. Minutes = int32.maxvalue)
{
Context. server. Transfer (rawurl. tolower (). Replace (". aspx", ". html "));
}
Else
{
Fileinfo = new fileinfo (PATH );
If (fileinfo. lastwritetime. addminutes (double) urlitem. Minutes) <datetime. Now)
{
Fileinfo. Delete ();
// Asp.net 1.1 uses the following method to compile the page
// Pageparser. getcompiledpageinstance (filepath, inputfile, context). processrequest (context );
Ihttphandler hander = buildmanager. createinstancefromvirtualpath (filepath, typeof (page) as ihttphandler;
Hander. processrequest (context );
Context. response. Filter = new aspxboy. buildhtmldemo. tohtmlfilter (context. response. filter, PATH );
}
Else
{
Context. server. Transfer (rawurl. tolower (). Replace (". aspx", ". html "));
}
Return;
}
} Sample code http://files.cnblogs.com/huobazi/BuildHtmlDemo.rar