知识点:
UrlRewrite、
IHttpModule、
IHttpHander 的编写
效果:
http://www.devedu.com/Doc/DotNet/AspNet/default.2.aspx
http://www.devedu.com/Doc/DotNet/AspNet/default.2.html
思路:
1 挂载“.aspx"的请求到自定义的Httphander内
2 配置URL重写规则
3 访问某.aspx文件时,在HttpHander内 根据配置确定是否应该生成
接着...
if(需要生成)
{
if(若已经生成html文件 )
{
if(文件并未过期)
{
则直接定向(Server.Transfer())。
}
else
{
删除HTML文件;
重新编译.aspx(Page内数据库操作等等)
生成HTML文件;
}
}
else if(尚未生成文件)
{
生成Html。
}
}
else
{
则编译.aspx文件
}
示例项目下载:
http://www.cnblogs.com/Files/huobazi/BuildHtmlDemo.rar
效果:
http://www.devedu.com/Doc/DotNet/AspNet/default.2.aspx
http://www.devedu.com/Doc/DotNet/AspNet/default.2.html
思路:
1 挂载“.aspx"的请求到自定义的Httphander内
2 配置URL重写规则
3 访问某.aspx文件时,在HttpHander内 根据配置确定是否应该生成
接着...
if(需要生成)
{
if(若已经生成html文件 )
{
if(文件并未过期)
{
则直接定向(Server.Transfer())。
}
else
{
删除HTML文件;
重新编译.aspx(Page内数据库操作等等)
生成HTML文件;
}
}
else if(尚未生成文件)
{
生成Html。
}
}
else
{
则编译.aspx文件
}
另:建议阅读一下dudu的blog中关于asp.net页面编译的讨论
http://www.cnblogs.com/dudu/archive/2006/03/07/345107.html
http://www.cnblogs.com/dudu/archive/2006/03/07/344351.html
部分代码
C#代码
- publicvoidProcessRequest(HttpContextcontext)
- {
- stringrawUrl=context.Request.RawUrl;
- stringrequestPath=context.Request.Path;
- stringapplicationPath=context.Request.ApplicationPath;
- UrlurlItem=null;
- //上下文中没有定义ToStaticUrlItem表示,此请求没有经过UrlRewrite,直接编译,不生成html
- //参考UrlRewriteModule.cs
- if(context.Items["ToStaticUrlItem"]==null)
- {
- if(!File.Exists(context.Request.PhysicalPath))
- {
- thrownewHttpException(404,"您访问的页面没有找到。");
- }
- //asp.net1.1采用下面方法编译页面
- //PageParser.GetCompiledPageInstance(requestPath,context.Request.PhysicalPath,context).ProcessRequest(context);
- IHttpHandlerhander=BuildManager.CreateInstanceFromVirtualPath(requestPath,typeof(Page))asIHttpHandler;
- hander.ProcessRequest(context);
- return;
- }
- stringfilePath;
- urlItem=(Url)context.Items["ToStaticUrlItem"];
- Regexregex=newRegex(
- Globals.ApplicationPath+urlItem.LookFor,
- RegexOptions.CultureInvariant|RegexOptions.Singleline|RegexOptions.Compiled|RegexOptions.IgnoreCase);
- stringrequestFile=regex.Replace(rawUrl,Globals.ApplicationPath+urlItem.WriteTo.Replace("^","&"));
- if(requestFile.IndexOf("?")>0)
- {
- filePath=requestFile.Substring(0,requestFile.IndexOf("?"));
- }
- else
- {
- filePath=requestFile;
- }
- stringinputFile=context.Request.PhysicalApplicationPath+filePath;
- stringpath=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.net1.1采用下面方法编译页面
- //PageParser.GetCompiledPageInstance(filePath,inputFile,context).ProcessRequest(context);
- IHttpHandlerhander=BuildManager.CreateInstanceFromVirtualPath(filePath,typeof(Page))asIHttpHandler;
- hander.ProcessRequest(context);
- return;
- }
- if(!File.Exists(path))
- {
- //asp.net1.1采用下面方法编译页面
- //PageParser.GetCompiledPageInstance(filePath,inputFile,context).ProcessRequest(context);
- IHttpHandlerhander=BuildManager.CreateInstanceFromVirtualPath(filePath,typeof(Page))asIHttpHandler;
- hander.ProcessRequest(context);
- context.Response.Filter=newAspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter,path);
- return;
- }
- if(urlItem.Minutes==Int32.MaxValue)
- {
- context.Server.Transfer(rawUrl.ToLower().Replace(".aspx",".html"));
- }
- else
- {
- FileInfofileInfo=newFileInfo(path);
- if(fileInfo.LastWriteTime.AddMinutes((double)urlItem.Minutes)<DateTime.Now)
- {
- fileInfo.Delete();
- //asp.net1.1采用下面方法编译页面
- //PageParser.GetCompiledPageInstance(filePath,inputFile,context).ProcessRequest(context);
- IHttpHandlerhander=BuildManager.CreateInstanceFromVirtualPath(filePath,typeof(Page))asIHttpHandler;
- hander.ProcessRequest(context);
- context.Response.Filter=newAspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter,path);
- }
- else
- {
- context.Server.Transfer(rawUrl.ToLower().Replace(".aspx",".html"));
- }
- return;
- }
- }