日期:2014-05-17  浏览次数:20899 次

asp.net配置全局应用程序类 巧妙达到定时生成静态页面
    //在项目里添加一个"全局应用程序类(Global Application Class)",在里面写这样的代码:
    public class Global : System.Web.HttpApplication
    {
        static Timer BuildStaticPagesTimer;
        static object locker = new object();
        static int count;

        protected void Application_Start(object sender, EventArgs e)
        {
            //double check lock...
            if (BuildStaticPagesTimer == null)
            {
                lock (locker)
                {
                    if (BuildStaticPagesTimer == null)
                    {
                        //every 20 minutes, run BuildStaticPagesTimer_Callback in every 20 minutes
                        BuildStaticPagesTimer = new Timer(BuildStaticPagesTimer_Callback, null, 0, 20 * 60 * 1000);
                    }
                }
            }
        }

        private static void BuildStaticPagesTimer_Callback(object state)
        {
            Dictionary<string, string> urlsNeedToBuild = GetPagesNeedToBuiltStatic();
            foreach (string oldUrl in urlsNeedToBuild.Keys)
            {
                string newUrl = urlsNeedToBuild[oldUrl];
                Build(oldUrl, newUrl);
            }
        }

        private static void Build(string oldUrl, string newUrl)
        {
            //在这里写生成静态页面的代码
            throw new NotImplementedException();
        }

        private static Dictionary<string, string> GetPagesNeedToBuiltStatic()
        {
            //在这里判断哪些页面需要生成静态页面
            throw new NotImplementedException();
        }
    }