Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件

很多朋友可能都讨论过ASP.NET中生成HTML的方法了,有按照模板用IO方法写文件
有在404错误页面内生成HTML的,有在Render内保存页面输出到HTML文件的。
今天我发一个配合Url重写利用HttpHander手工编译.aspx页面方法。
HTML文件的方法,可按需、“定时”的生成,以减轻数据库的访问。

声明:下面的文字是本文不可缺少的部分,转载请保留,谢谢!
////////////////////////////////////////////////////
作者:武眉博<活靶子.NET>
同时首发于:
    
落伍者   && 博客园  
    
开发者学院   && .Net男孩社区
知识点:UrlRewriteIHttpModuleIHttpHander 的编写
效果:
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#代码
  1 public void ProcessRequest(HttpContext context)    
  2         {    
  3             string rawUrl = context.Request.RawUrl;    
  4             string requestPath = context.Request.Path;    
  5             string applicationPath = context.Request.ApplicationPath;    
  6             Url urlItem = null;    
  7    
  8             //上下文中没有定义ToStaticUrlItem表示,此请求没有经过UrlRewrite,直接编译,不生成html    
  9             //参考UrlRewriteModule.cs    
 10             if (context.Items["ToStaticUrlItem"== null)    
 11             {    
 12                 if (!File.Exists(context.Request.PhysicalPath))    
 13                 {    
 14                     throw new HttpException(404"您访问的页面没有找到。");    
 15                 }    
 16    
 17                 // asp.net 1.1 采用下面方法编译页面    
 18                 //PageParser.GetCompiledPageInstance(requestPath, context.Request.PhysicalPath, context).ProcessRequest(context);    
 19                 IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(requestPath, typeof(Page)) as IHttpHandler;    
 20                 hander.ProcessRequest(context);    
 21    
 22                 return;    
 23             }    
 24    
 25             string filePath;    
 26    
 27             urlItem = (Url)context.Items["ToStaticUrlItem"];    
 28    
 29             Regex regex = new Regex(    
 30                 Globals.ApplicationPath + urlItem.LookFor,    
 31                 RegexOptions.CultureInvariant | RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);    
 32    
 33             string requestFile = regex.Replace(rawUrl, Globals.ApplicationPath + urlItem.WriteTo.Replace("^""&"));    
 34    
 35             if (requestFile.IndexOf("?"> 0)    
 36             {    
 37                 filePath = requestFile.Substring(0, requestFile.IndexOf("?"));    
 38             }    
 39             else   
 40             {    
 41                 filePath = requestFile;    
 42             }    
 43    
 44             string inputFile = context.Request.PhysicalApplicationPath + filePath;    
 45             string path = context.Request.PhysicalApplicationPath + rawUrl.ToLower().Replace(".aspx"".html");    
 46             if (applicationPath != "/")    
 47             {    
 48                 inputFile = inputFile.Replace(applicationPath + "/"@"\");   
 49                 path = path.Replace(applicationPath + "/""").Replace("/"@"\");   
 50             }   
 51             else   
 52             {   
 53                 path = path.Replace("/"@"\");   
 54             }   
 55   
 56             if (!urlItem.EnabledToStatic)   
 57             {   
 58                 // asp.net 1.1 采用下面方法编译页面   
 59                 //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );   
 60                 IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;   
 61                 hander.ProcessRequest(context);   
 62   
 63                 return;   
 64             }   
 65   
 66             if (!File.Exists(path))   
 67             {   
 68                 // asp.net 1.1 采用下面方法编译页面   
 69                 //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );   
 70                 IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;   
 71                 hander.ProcessRequest(context);   
 72                 context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);   
 73   
 74                 return;   
 75             }   
 76   
 77             if (urlItem.Minutes == Int32.MaxValue)   
 78             {   
 79                 context.Server.Transfer(rawUrl.ToLower().Replace(".aspx"".html"));   
 80             }   
 81             else   
 82             {   
 83                 FileInfo fileInfo = new FileInfo(path);   
 84                 if (fileInfo.LastWriteTime.AddMinutes((double)urlItem.Minutes) < DateTime.Now)   
 85                 {   
 86                     fileInfo.Delete();   
 87   
 88                     // asp.net 1.1 采用下面方法编译页面   
 89                     //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );   
 90                     IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;   
 91                     hander.ProcessRequest(context);   
 92                     context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);   
 93                 }   
 94                 else   
 95                 {   
 96                     context.Server.Transfer(rawUrl.ToLower().Replace(".aspx"".html"));    
 97                 }    
 98                 return;    
 99             }    
100         }   
101 


示例项目下载:https://files.cnblogs.com/huobazi/BuildHtmlDemo.rar


本文来自:http://www.cnblogs.com/fengfeng/archive/2008/01/30/1058890.html
posted @ 2008-01-31 09:42  herobeast  阅读(1272)  评论(0编辑  收藏  举报