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

.NET生成静态页面
求一段将动态页面转换为静态页面的方法。
越简单越好。网站日访问量实在很大。

------解决方案--------------------
生成HTML
C# code
public static bool CreatHtmlPage(string[] strNewsHtml, string[] strOldHtml, string strModeFilePath, string strPageFilePath)
        {
            bool Flage = false;
            StreamReader ReaderFile = null;
            StreamWriter WrirteFile = null;
            //修改mode.htm到inc目录下
           strModeFilePath = "../inc/" + strModeFilePath;
            string FilePath = HttpContext.Current.Server.MapPath(strModeFilePath);
            Encoding Code = Encoding.GetEncoding("gb2312");
            string strFile = string.Empty;
            try
            {
                ReaderFile = new StreamReader(FilePath, Code);
                strFile = ReaderFile.ReadToEnd();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ReaderFile.Close();
            }
            try
            {
                int intLengTh = strNewsHtml.Length;
                for (int i = 0; i < intLengTh; i++)
                {
                    strFile = strFile.Replace(strOldHtml[i], strNewsHtml[i]);
                }
                WrirteFile = new StreamWriter(HttpContext.Current.Server.MapPath(strPageFilePath), false, Code);
                WrirteFile.Write(strFile);
                Flage = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

                WrirteFile.Flush();
                WrirteFile.Close();
            }
            return Flage;
        }
        public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)
        {
            bool Flage = false;
            StreamReader ReaderFile = null;
            StreamWriter WrirteFile = null;
            string FilePath = HttpContext.Current.Server.MapPath(strHtml);
            Encoding Code = Encoding.GetEncoding("gb2312");
            string strFile = string.Empty;
            try
            {
                ReaderFile = new StreamReader(FilePath, Code);
                strFile = ReaderFile.ReadToEnd();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ReaderFile.Close();
            }
            try
            {
                int intLengTh = strNewsHtml.Length;
                for (int i = 0; i < intLengTh; i++)
                {
                    int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
                    int intEnd = strFile.IndexOf(strEndHtml[i]);
                    string strOldHtml = strFile.Substring(intStart, intEnd - intStart);
                    strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
                }
                WrirteFile = new StreamWriter(FilePath, false, Code);
                WrirteFile.Write(strFile);
                Flage = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

                WrirteFile.Flush();
                WrirteFile.Close();
            }
            return Flage;
        }