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

2016/12/29 15:32
阅读数 6
//在项目里添加一个"全局应用程序类(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();
        }
    }

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部