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

时间:2023-03-09 06:37:56
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. protected void Application_Start(object sender, EventArgs e)
  8. {
  9. //double check lock...
  10. if (BuildStaticPagesTimer == null)
  11. {
  12. lock (locker)
  13. {
  14. if (BuildStaticPagesTimer == null)
  15. {
  16. //every 20 minutes, run BuildStaticPagesTimer_Callback in every 20 minutes
  17. BuildStaticPagesTimer = new Timer(BuildStaticPagesTimer_Callback, null, 0, 20 * 60 * 1000);
  18. }
  19. }
  20. }
  21. }
  22. private static void BuildStaticPagesTimer_Callback(object state)
  23. {
  24. Dictionary<string, string> urlsNeedToBuild = GetPagesNeedToBuiltStatic();
  25. foreach (string oldUrl in urlsNeedToBuild.Keys)
  26. {
  27. string newUrl = urlsNeedToBuild[oldUrl];
  28. Build(oldUrl, newUrl);
  29. }
  30. }
  31. private static void Build(string oldUrl, string newUrl)
  32. {
  33. //在这里写生成静态页面的代码
  34. throw new NotImplementedException();
  35. }
  36. private static Dictionary<string, string> GetPagesNeedToBuiltStatic()
  37. {
  38. //在这里判断哪些页面需要生成静态页面
  39. throw new NotImplementedException();
  40. }
  41. }