几种静态页面生成的代码,包括带内容分页的生成静态

        #region 直接读取页面生成静态
        /// <summary>
        /// 直接读取页面生成静态
        /// </summary>
        /// <param name="Url">需要生成静态的页面</param>
        /// <param name="htmlPath">保存静态页面的地址</param>
        private void toHtmlPath(string Url, string htmlPath)
        {
            string WebUrl = Url;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream stream = null;
            StreamReader reader = null;
            request = (HttpWebRequest)WebRequest.Create(WebUrl);                 
            response = (HttpWebResponse)request.GetResponse();
            stream = response.GetResponseStream();
            reader = new StreamReader(stream);
            string Result = reader.ReadToEnd();
            reader.Close();
            response.Close();

            string savePath = Server.MapPath("~/HtmlPage/" + htmlPath);
            StreamWriter fileWriter = new StreamWriter(savePath, false, Encoding.UTF8);
            fileWriter.Write(Result);
            fileWriter.Close();
        }
        #endregion

 

这个方法适合于首页或频道页面的生成,因为这些页面的内容可以通过程序直接做出来,不用模板来替换,因此,直接读取页面内容,然后把读取到的HTML代码直接写成一个Html文件,就生成了静态页面。

调用:

toHtmlPath("http://www.hao123.com","hao123.html")

 

以上方法一次就生成一个静态页面,如果对于页面内容不规则,但是又带了参数ID的页面,只需要循环生成就行了。

例如:

            DataTable dt = new DataTable();
            dt = new A().B();  //读取数据库表放入dt
            foreach (DataRow row in dt.Rows)
            {//循环每行内容,以ID作为不同页面名称
                string ID = row["ID"].ToString();
              toHtmlPath("http://www.uhuibao.com/A.aspx?id=" + ID, "A" + ID + ".html"); //写入到html文件
            }

 

新闻资讯页面比较规则,写入可以用模板替换的方式,先做一个html的模板页,标题,来源,时间,内容,分页等都用标识符号做好标签,例如:{@Title}

然后我们需要读取模板内容放入变量,然后再从数据库中读取新闻内容,对相对应的标签做替换,最后生成html.

不带内容分页代码如下:

            string TempLatePath = Server.MapPath("~/Template/NewsTemp.html"); //模板文件所在目录
            if (File.Exists(TempLatePath)) //判断模板文件是否存在
            {
                StreamReader Reader = new StreamReader(TempLatePath, Encoding.UTF8);  //实例化读取模板文件对象
                string TempLateValue = Reader.ReadToEnd();//读取模板文件内容,从头读到尾,放入一个变量
                Reader.Close(); //关闭对象

                DataTable dt = new NewsManage().getAllNews();   //从数据库中读取指定的新闻列表
                foreach (DataRow row in dt.Rows)                //一个一个开始循环并取出内容
                {
                    StringBuilder Result = new StringBuilder(TempLateValue);          //建立字符串对象
                    Result = Result.Replace("{@Title}", row["Title"].ToString());     //替换标题标签
                    Result = Result.Replace("{@Time}", row["PublishDateTime"].ToString());//替换发布时间标签
                    Result = Result.Replace("{@From}", row["TransURL"].ToString());//替换来源标签
                    Result = Result.Replace("{@Content}", row["Contents"].ToString());//替换内容标签

                    string NewsId = row["ID"].ToString();                             //ID作为识别

                    string savePath = Server.MapPath("~/Html_News/NewsShow_" + NewsId + ".html");   //一个ID写入对应的一个html文件
                    StreamWriter WriteFile = new StreamWriter(savePath, false, Encoding.UTF8);     //写入
                    WriteFile.Write(Result.ToString());
                    WriteFile.Close();
                }
                lblMessage.Text = "新闻内容静态页面生成成功!";
            }

 带内容分页代码:

protected void btnHtml_Click(object sender, EventArgs e)
        {
            string TempLatePath = Server.MapPath("~/Template/Temp.htm");   //模板文件所在目录
            string Content = "AAAAAAAAAAA<div>[NextPage]</div><br />BBBBBBBBBB<div>[NextPage]</div><br />CCCCCCCCC<div>[NextPage]</div>DDDDDDDDDD<div>[NextPage]</div>EEEEEEEEEEE";    //示例中需要用到的新闻内容

            #region 内容分页
            //因为有分页标识符(fck编辑器带有这个功能,自己做过修改),用分隔函数把带有分隔的分开放入数组
            string[] txtList = Content.ToString().Split(new string[] { "<div>[NextPage]</div>" }, StringSplitOptions.RemoveEmptyEntries);           
            //获取数组长度
            int PageRecord = int.Parse(txtList.Length.ToString());

            #endregion

            for (int i = 0; i < PageRecord; i++) //循环分页内容
            {
                if (File.Exists(TempLatePath))   //判断模板文件是否存在
                {
                    StreamReader Reader = new StreamReader(TempLatePath, Encoding.UTF8);
                    string TempLateValue = Reader.ReadToEnd();
                    Reader.Close();

                    StringBuilder Result = new StringBuilder(TempLateValue);   //模板内容填充到字符串对象中
                    StringBuilder sb = new StringBuilder();                    //再次实例化一个字符串对象

                    if (i == txtList.GetLowerBound(0))       //如果当前页为首页,“首页”就不加链接              
                    {
                        sb.Append("首页 ");
                    }
                    else   //如果当前页不为首页,“首页”就加链接     
                    {
                        sb.Append("<a href="NewsShow_" mce_href="NewsShow_"" + (txtList.GetLowerBound(0) + 1) + ".html>首页</a> ");
                    }                    
                  
                    for (int j = 1; j <= PageRecord; j++)   //再次循环分页标识内容
                    {
                        if (j == (i + 1))                  //当前页的对应数字加样式
                        {
                            sb.Append("<a href="NewsShow_" mce_href="NewsShow_"" + j + ".html style="color:red;font-size:14px;" mce_style="color:red;font-size:14px;">" + j + "</a> ");
                        }
                        else
                        {
                            sb.Append("<a href="NewsShow_" mce_href="NewsShow_"" + j + ".html>" + j + "</a> ");
                        }
                        
                    }

                    if (i == txtList.GetUpperBound(0))   
                    {
                        sb.Append("尾页 ");
                    }
                    else
                    {
                        sb.Append("<a href="NewsShow_" mce_href="NewsShow_"" + (txtList.GetUpperBound(0) + 1) + ".html>尾页</a> ");
                    }
                    

                    Result = Result.Replace("{@Content}", txtList[i]);    //替换内容标签

                    Result = Result.Replace("{@Page}",  sb.ToString());   //替换分页标签

                    string savePath = Server.MapPath("~/NewsShow_" + (i+1) + ".html");   //设置保存位置
                    StreamWriter WriteFile = new StreamWriter(savePath, false, Encoding.UTF8);  //写入保存
                    WriteFile.Write(Result.ToString());
                    WriteFile.Close();
                }
            }
        }

 以上这段代码还没完善,因为还可以加入增量生成或全部生成的判断。暂时先写到这。

posted @ 2012-08-02 09:49  佛缘  阅读(722)  评论(0编辑  收藏  举报