關於靜態頁面生成的研究

FileMaker:   

 public class FileMaker
    {
        public FileMaker()
        { }

        private static bool NeedMake(string AppPath, int timeout)
        {
            //如果文件不存在,就生成它。
            if (File.Exists(AppPath) == false) return true;

            //沒啓用超時,不生成。
            if (timeout == 0) return false;

            //如果過期,生成。
            DateTime dt = File.GetLastWriteTime(AppPath);
            if (dt.AddMinutes(timeout) <= DateTime.Now)
                return true;
           

            return false;
        }


        /// <summary>
        /// Make
        /// </summary>
        /// <param name="url"></param>
        /// <param name="toPath"></param>
        public static void Make(string url,int timeout)
        {
           HttpContext context = HttpContext.Current;
           string AppPath = context.Request.PhysicalPath;

           if (NeedMake(AppPath, timeout) == false) return;

           StreamWriter sw = new StreamWriter(AppPath, false, Encoding.GetEncoding("GB2312"));
           try
           {
               context.Server.Execute(url, sw);
           }
           catch {
          
           }
          
            sw.Close();
                     

        }
    } 

HttpModule :


 public class HttpModule:IHttpModule
 {
  public void Init(HttpApplication app)
  {
     app.BeginRequest += new EventHandler(this.ModuleRewriter_BeginRequest);

  }

public void ModuleRewriter_BeginRequest(Object source, EventArgs e)
  {
   HttpApplication application = (HttpApplication) source;
   HttpContext context = application.Context;

   string newPath = null;
   string path = context.Request.Path;
   int timeout;
    bool isReWritten = RewriteUrl(path, out newPath, out timeout);

    if (isReWritten && newPath != null)
   {
                FileMaker.Make(newPath, timeout);

   }

}

public static bool RewriteUrl(string path,out string newPath,out int timeout)
  {
   SiteUrls siteurl = Global.GetSiteUrls();//具體實現不在描述
   ArrayList urls = siteurl.ReWrittenUrls;//具體實現不在描述
   if (urls.Count > 0)
   {
    foreach (ReWrittenUrl url in urls)
    {
     if (url.IsMatch(path))
     {
      newPath = url.Convert(path);
                        timeout = url.TimeOut;
      return true;
     }
     
    }
    
   }
   
   newPath = null;
            timeout = 0;
   return false;
  }

 }

 

URLCONFIG.CONFIG

ForExample:

<?xml version="1.0" encoding="utf-8" ?>
<urls>

        <!--
        ****************
        注意, 在url查詢字符串中,字符 ^ 將被字符 & 替換.
        ****************
        -->
  <url name="cat_show" timeout="20" pattern="~/Cat/(/d+).aspx" vanity="~/SrvPort/class_list.aspx?class_id=$1"/>

  <url name="aritcle_show" timeout="0" pattern="~/File/(/d+).aspx" vanity="~/SrvPort/article_show.aspx?id=$1"/>

 </urls>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章