asp.net配置全局应用程序类 巧妙达到定时生成静态页面

  1. //在项目里添加一个"全局应用程序类(Global Application Class)",在里面写这样的代码:  
  2. public class Global : System.Web.HttpApplication  
  3. {  
  4.     static Timer BuildStaticPagesTimer;  
  5.     static object locker = new object();  
  6.     static int count;  
  7.   
  8.     protected void Application_Start(object sender, EventArgs e)  
  9.     {  
  10.         //double check lock...  
  11.         if (BuildStaticPagesTimer == null)  
  12.         {  
  13.             lock (locker)  
  14.             {  
  15.                 if (BuildStaticPagesTimer == null)  
  16.                 {  
  17.                     //every 20 minutes, run BuildStaticPagesTimer_Callback in every 20 minutes  
  18.                     BuildStaticPagesTimer = new Timer(BuildStaticPagesTimer_Callback, null, 0, 20 * 60 * 1000);  
  19.                 }  
  20.             }  
  21.         }  
  22.     }  
  23.   
  24.     private static void BuildStaticPagesTimer_Callback(object state)  
  25.     {  
  26.         Dictionary<stringstring> urlsNeedToBuild = GetPagesNeedToBuiltStatic();  
  27.         foreach (string oldUrl in urlsNeedToBuild.Keys)  
  28.         {  
  29.             string newUrl = urlsNeedToBuild[oldUrl];  
  30.             Build(oldUrl, newUrl);  
  31.         }  
  32.     }  
  33.   
  34.     private static void Build(string oldUrl, string newUrl)  
  35.     {  
  36.         //在这里写生成静态页面的代码  
  37.         throw new NotImplementedException();  
  38.     }  
  39.   
  40.     private static Dictionary<stringstring> GetPagesNeedToBuiltStatic()  
  41.     {  
  42.         //在这里判断哪些页面需要生成静态页面  
  43.         throw new NotImplementedException();  
  44.     }  
  45. }  
原文地址:https://www.cnblogs.com/codeloves/p/3338675.html