日期:2014-05-18  浏览次数:20504 次

关于静态页自动生成
我尝试了在Global.asax文件中定时执行程序
但是虚拟路径的问题给我卡住了
谁有成功的经验可以传授下
希望大哥们成功过再说。。别到处复制黏贴 谢谢。。。

------解决方案--------------------
就是替换标签
------解决方案--------------------
帮顶!
------解决方案--------------------
引用楼主 woshilaidarende 的帖子:
我尝试了在Global.asax文件中定时执行程序
但是虚拟路径的问题给我卡住了
谁有成功的经验可以传授下
希望大哥们成功过再说。。别到处复制黏贴 谢谢。。。

------解决方案--------------------
我路过来看一眼.顺道友情UP下
------解决方案--------------------
http://blog.csdn.net/qdzx2008/archive/2005/10/04/495209.aspx
------解决方案--------------------
在Global.asax

里面调用一个类
类里面 写个线程,,设置多久自动生成就可以了
不难
------解决方案--------------------
protected void Application_BeginRequest(object sender, EventArgs e)
{

if (!isStartBuilder)
{

Test.BuilderHtml bb = new zzy.Test.BuilderHtml();
bb.StartAutoBuilder();
}

}

C# code

 public  class BuilderHtml
    {

        public string RootUrl = WebUnits.WebRootUrl;
        public static string Serverpath = HttpContext.Current.Server.MapPath("/");


        public void StartAutoBuilder()
        {
            WriteLog(string.Format("StartAutoBuilder"));
            ThreadStart threadStart = new ThreadStart(AutoBuilder);
            Thread th = new Thread(threadStart);
            th.Start();
        }

        /// <  summary>
        ///10分钟生成一次
        /// <  /summary>
        public void AutoBuilder() 
        {
            int fasterBuilderTime = Int32.Parse(ConfigurationSettings.AppSettings["FasterBuilderTime"]);
            Thread.Sleep(fasterBuilderTime);
            BuildMethod();
        }
        private void BuildMethod()
        {
            int sleepTime = Int32.Parse(ConfigurationSettings.AppSettings["BuilderTime"]);
            while (true)
            {
                Thread.Sleep(sleepTime);
                BuildAllHtmlPage();
            }
        }

        /// <summary>
        /// 生成模块首页静态页面
        /// </summary>
        public void BuildAllHtmlPage()
        {
            ThreadStart threadStart = new ThreadStart(BuildHtmlPage);
            Thread th = new Thread(threadStart);
            th.Start();
        }

        private void BuildHtmlPage()
        {
            string[] htmlPages = ConfigurationSettings.AppSettings["HomeHtml"].ToString().Split(';');
            for (int i = 0; i < htmlPages.Length; i++)
            {
                string config = htmlPages[i];
                string[] arr = config.Split(',');
                string url = RootUrl + arr[0];
                string filePath = Serverpath + arr[1];
                try
                {
                    DBResult result = HtmlBuilder.CreateHtml(url, filePath);
                    if (!result.Succeed)
                        WriteLog(string.Format("生成失败:{0},{1},原因:{2}", url, filePath, result.Message));
                }
                catch (Exception ex)
                {
                    WriteLog(string.Format("生成失败:{0},{1},原因:{2}", url, filePath, ex.Message));
                }
            }
        }


        public static void WriteLog(string log)
        {
            try
            {
                string fileName = Serverpath + "log.txt";
                if (File.Exists(fileName))
                {
                    using (StreamWriter sw = new StreamWriter(fileName, true))
                    {
                        sw.WriteLine(
                            string.Format("\r\n==[{0}]==================