您的位置:首页 > 编程语言 > ASP

Asp.net 静态页面生成(1)----模板生成

2020-03-02 00:57 555 查看

周末了,写点开发经验大家分享
      废话少说,开门见山。静态页面生成技术是很多大网站采用的技术。用于大型网站的访问量特别高,采用动态页面难免服务器负担很重,负载大的时候可能down机。人们想出了采取以前的静态页面,这样可以减少服务器运算负载,但是一个一个页面的做肯定人工成本太高。于是乎就出现了静态页面生成技术。
      静态页面生成的实现方法大致可以分为两种
      一、模板生成
      实现思想:提前把网页的公共部分写好,做成一个模板,而不同的部分采用特殊字符代替。当需要生产的时候,用程序去读取模板,然后去数据库中找到需要的数据替换模板中的特殊字符。生成真正的网页然后存在网站的目录下。 

      有点:效率高,生成速度快。
      缺点:需要在网站建设之前就确定采用此方法生成。对于已建好的动态站点改版为此方法 工作量大。
      实例代码:
Article.Templete.txt

<!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>
    <title><@Title@></title>
</head>
<body>
    <table border="0" cellpadding="0" cellspacing="0" width="980">
        <tr>
            <td style="height: 200px">
                header
            </td>
        </tr>
        <tr>
            <td>
                <table border="0" cellpadding="0" cellspacing="0" width="980">
                    <tr>
                        <td>
                        <@Title@>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        <@Content@>
                        </td>
                    </tr>
                    <tr>
                        <td>
                        剪辑:<@Editor@>&nbsp;&nbsp;&nbsp;&nbsp;访问次数:<@Hits@>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td style="height: 100px">
                footer
            </td>
        </tr>
    </table>
</body>
</html>


生成函数

/**//// <summary>
    /// 生成文章方法
    /// </summary>
    /// <param name="templetefile">模板文件路径</param>
    /// <param name="articleId">文章Id</param>
    public void MakeArticleFile(string templetefile, int articleId)
     {
        string sqlStr = "SELECT * FROM Article WHERE ArticleId = " + articleId.ToString();
        DataSet ds = DataBase.ExecuteDataset(sqlStr);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
         {
            DataRow dr = ds.Tables[0].Rows[0];
            try
             {
                System.IO.StreamReader sr = new System.IO.StreamReader(templetefile, System.Text.Encoding.UTF8);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("/Article/" + dr["ArticleId"].ToString() + ".html"), false, System.Text.Encoding.UTF8);
                string templete = sr.ReadToEnd();
                sr.Close();
                sr.Dispose();
                templete = templete.Replace("<@Title@>", dr["Title"].ToString());
                templete = templete.Replace("<@Content@>", dr["Content"].ToString());
                templete = templete.Replace("<@Editor@>", dr["Editor"].ToString());
                templete = templete.Replace("<@Hits@>", dr["Hits"].ToString());
                sw.Write(templete);
                sw.Flush();
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
             {
                Response.Write(ex.Message);
            }
        }
    }

转载于:https://www.cnblogs.com/michael555cdj/archive/2008/03/22/1118117.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
dianqiong9604 发布了0 篇原创文章 · 获赞 0 · 访问量 22 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: