现在的位置: 首页 > 综合 > 正文

利用HttpHander手工编译页面并按需生成静态HTML文件

2013年12月06日 ⁄ 综合 ⁄ 共 4496字 ⁄ 字号 评论关闭
 知识点: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文件
}

部分代码

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.              if (!urlItem.EnabledToStatic)
  56.              {
  57.                  // asp.net 1.1 采用下面方法编译页面
  58.                  //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );
  59.                  IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;
  60.                  hander.ProcessRequest(context);
  61.                  return;
  62.              }
  63.              if (!File.Exists(path))
  64.              {
  65.                  // asp.net 1.1 采用下面方法编译页面
  66.                  //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );
  67.                  IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;
  68.                  hander.ProcessRequest(context);
  69.                  context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);
  70.                  return;
  71.              }
  72.              if (urlItem.Minutes == Int32.MaxValue)
  73.              {
  74.                  context.Server.Transfer(rawUrl.ToLower().Replace(".aspx", ".html"));
  75.              }
  76.              else
  77.              {
  78.                  FileInfo fileInfo = new FileInfo(path);
  79.                  if (fileInfo.LastWriteTime.AddMinutes((double)urlItem.Minutes) < DateTime.Now)
  80.                  {
  81.                      fileInfo.Delete();
  82.                      // asp.net 1.1 采用下面方法编译页面
  83.                      //PageParser.GetCompiledPageInstance( filePath , inputFile , context ).ProcessRequest( context );
  84.                      IHttpHandler hander = BuildManager.CreateInstanceFromVirtualPath(filePath, typeof(Page)) as IHttpHandler;
  85.                      hander.ProcessRequest(context);
  86.                      context.Response.Filter = new AspxBoy.BuildHtmlDemo.ToHtmlFilter(context.Response.Filter, path);
  87.                  }
  88.                  else
  89.                  {
  90.                      context.Server.Transfer(rawUrl.ToLower().Replace(".aspx", ".html"));   
  91.                  }   
  92.                 return;   
  93.              }   
  94.          }  

抱歉!评论已关闭.