独上高楼网站
  •    你所在位置:首页 VS.netASP.net编程经验〉ASP.Net自动批量生成HTML页面和索引页
  • ASP.Net自动批量生成HTML页面和索引页
  • 作者:王宏喜  文章来源:原创  发布日期:2007-06-05  浏览次数:1182
  • 打印这篇文章
  • ASP.Net自动批量生成HTML页面和索引页

    (泰州市国土资源局,江苏 泰州 225300

    2007.6.5

     

            前天在园子里把在ASP.net中网站访问量统计方法 一文贴了上去,觉得对刚学做网站的朋友还有点用处,也有好多朋友留言鼓励,也有朋友指出了存在的问题,对我很受启发。今天是休息日,我就把在“独上高楼”网站上原创一这篇文章又贴了上来,敬请朋友指教,本文是参考了大型网站新闻系统如何生成静态网页ASP.NET生成静态网页的方法 等文章后形成的思路,同时在本人网站上实现了批量转换。以下所发布的代码均为我个人网站实际运行的代码,读者可以作适当的修改后相信能在你的系统中运行。

    本网站新闻分类为三级,即大类、小类、三级类,主页面为VS.net动态页面,要实现一次性自动批量生成HTML页面和索引页面,需要解决如下问题:

    1.在添加和修改文章时,自动生成HTML页面;

    2.根据新闻分类自动判断和创建存储HTML页面的目录;

    3.按三级类、小类、大类自动批量生成HTML页面,或者一次性生成所有的HTML页面和HTML索引页面;

    一、根据新闻分类自动判断和创建存储HTML页面的目录

    在添加和修改新闻时,需要对新闻所属类别进行选择或者确认,本网站就是根据新闻三级分类来创建存储HTML页面目录的,所有索引目录存储在“HTML”目录下,所有HTML文件存储在对应的第三级目录下,如123.html存储在:../ntml/1/11/111/123.htmlHTML文件名均采用文章的ID编号命名。

    自动判断和创建存储HTML页面的目录的代码如下,解释包含在代码中,这里不重复了:

         //这里自动生成Html文件

         //首先判断是否要创建按照ID三级文件夹

         string ClassPathA = this.txtClass1.Text.Trim();  // ClassPathA为大类码

         string ClassPathB = this.txtClass2.Text.Trim();  // ClassPathB为小类码

         string ClassPathC = this.txtClass3.Text.Trim();  // ClassPathC为三级类码

         string news_ClassPath = "html/" + ClassPathA + "/" + ClassPathB + "/" + ClassPathC + "/";

         //HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。

         if (!File.Exists(HttpContext.Current.Server.MapPath("html/") + ClassPathA))

         {

          //System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。

          System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("html/") + "\\" + ClassPathA);

         }

         if (!File.Exists(HttpContext.Current.Server.MapPath("html/" + ClassPathA) + ClassPathB))

         {

          System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("html/" + ClassPathA) + "\\" + ClassPathB);

         }

         if (!File.Exists(HttpContext.Current.Server.MapPath("html/" + ClassPathA + "/" + ClassPathB) + ClassPathC))

         {

              System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("html/" + ClassPathA + "/" + ClassPathB) + "\\" + ClassPathC);

     }

     

    二、在添加和修改文章时,自动生成HTML文件

    在添加和修改新闻文章时,可以生动生成HTML页面,也可以留待以后生成。本网站在添加或者修改一篇文章后是自动生成HTML页面的,代码如下:

          //自动生成Html文件

          string news_title=this.txtTitle.Text;

          string strBody = Server.HtmlDecode(this.FreeTextBox1.Text.Trim());

          strBody = strBody.Replace(System.Web.HttpUtility.HtmlDecode(" "), " "); //去掉代替空格显示的“?”号

           string news_Body = strBody;

           string news_id=Request.QueryString["articleID_int"].ToString();

           string news_Writer=this.txtWriter.Text;

           string news_Source = this.txtsource.Text;

           string news_Dateantime = DateTime.Now.ToString("yyyy-MM-dd   HH:mm:ss");

           bool result = HtmlDB.WriteFile_1(news_title, news_Body, news_id, news_Writer, news_Source, news_Dateantime, news_ClassPath);

           if (result)

           {

               Response.Write("scriptalert(\"生成HTML页面成功\");/script");

           }

           else

           {

       }

    这里的关键是黑体字部分,因为需要把HtmlDecode中的一些" "符号转换为空格,还有一些不明不白的符号,我也弄不清楚,必需要转换一下,修改时从数据库中读取也是一样需要转换。其他的代码相信大家能看懂的。

     

    三、自动批量生成HTML页面和索引页

    需要说明的是,本程序首先由新闻数据库创建了一个强类型的DataSet,以下代码均针对强类型数据集进行操作,关于强类型数据集不在本文论讨的范围之内,你可以在这里得到有关创建数据访问层和强类型的知识(http://whx.tzgt.gov.cn/newshow/newBodyShow.aspx?articleID=688),里不加表述。如果对强类型数据集不熟悉,也可以直接对数据库进行操作以获取数据。

    1.创建一个HTML页面模板

    创建一个名为HtmlModule.htm模板,用以统一所生成的HTML页面的格式,创建一个名叫HtmlModuleAllNews.htm的模板页面,用于生成索引页面。在创建HTML页面时系统实际上是读取该模板,然后添加替换有关内容后而生成HTML页面的。关于HTML页面模板你可以自己做一个HTML页面,由于我的HtmlModule.htm模板代码较大,所以用索引页面的模板HtmlModuleAllNews.htm示出

    !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

    html xmlns="http://www.w3.org/1999/xhtml"

    head

        titleASP.Net自动批量生成HTML页面和索引页/title

        meta http-equiv="Content-Type" content="text/html; charset=gb2312" /

    /head

    body

     table style="width: 800px; border-right: #666699 1px solid; border-top: #666699 1px solid; border-left: #666699 1px solid; border-bottom: #666699 2px solid;"

            tr

                td align="center" style="font-size: 24px; color: #ffffff; height: 60px; background-color: #669999"

                    ASP.Net自动批量生成HTML页面和索引页

                /td

            /tr

            tr

                td style="background-color: #e3ffff"

                    br /

                    {content}

                /td

            /tr

            tr

                td style="height: 80px; background-color: #99cc99" align="center"

                    span style="color: #003366"泰州市国土资源局/span span style="color: #ff0066"王宏喜br /

                        span style="color: #660033"地址/span〉〈span style="color: #000000"/span〉〈span

                            style="color: #000066"泰州市凤凰东路号/span〉〈span style="color: #000000" /span

                        span style="color: #990066"邮编/span〉〈span style="color: #000000"br /

                            span style="color: #990066"电话/spanspan style="color: #000066"0523-6883033 电子邮件/spanspan

                                style="color: #0000ff"Tzgtwhx@163.com  span style="color: #ff0066"QQ/span〉〈/span〉〈/span〉〈/span〉〈/td

            /tr

        /table

     

    /body

    /html

    注意,加黑地方的ASP.Net自动批量生成HTML页面和索引页{content}就是需要替换的内容。

     

    2.创建一个自动生成Html页面的的公共类

    创建一个名叫HtmlDB.cs的公共类,它的作用主要是生成Html页面和索引页面。其类代码如下:

    using System;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Text;

    using System.IO;

    ///

    /// DB 的摘要说明

    ///

    public class HtmlDB

    {

         public HtmlDB()

         {

             //

             // TODO: 在此处添加构造函数逻辑

             //

         }

      //以下WriteFile_1()用于生成HTML页面

        public static bool WriteFile_1(string news_title, string news_Body, string news_id, string news_Writer, string news_Source, string news_Dateantime, string news_ClassPath)

        {

            //申明字符编码,命名空间System.Text

            Encoding encoding = Encoding.GetEncoding("gb2312");

            //读取模板文件

            string TemplatePath = HttpContext.Current.Server.MapPath("HtmlModule.htm");  //

            //StreamReader实现一个 TextReader,使其以一种特定的编码从字节流中读取字符,命名空间System.IO

            StreamReader sr = null;

            //StreamWriter实现一个 TextWriter,使其以一种特定的编码向流中写入字符,命名空间Syste.IO

            StreamWriter sw = null;

            string str = "";

            try

            {

                sr = new StreamReader(TemplatePath, encoding);

     

                //从流的当前位置到末尾读取流

                str = sr.ReadToEnd();

            }

            catch (Exception e1)

            {

                HttpContext.Current.Response.Write(e1.Message);

                HttpContext.Current.Response.End();

            }

            finally

            { 

                sr.Close();

                sr.Dispose();

            }

            //生成路径

            //string myPath = "html/";

            string myPath = news_ClassPath;

            string OutPutPath = HttpContext.Current.Server.MapPath(myPath);

            //生成的文件名

            //string newName = DateTime.Now.ToString("yyyyMM_") + news_id + ".html";

            string newName = news_id + ".html";

            //替换内容

            str = str.Replace("{title}", news_title);

            str = str.Replace("{content}", news_Body);

            str = str.Replace("ASP.Net自动批量生成HTML页面和索引页", news_title);

            str = str.Replace("王宏喜", news_Writer);