职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 2822|回复: 0

asp.net(c#)生成静态html页面

[复制链接]
梦段桥 发表于 2007-9-13 17:34 | 显示全部楼层 |阅读模式
生成静态页面的好处就很多了,比如提高访问速度,利于搜索引擎抓取等等。下面这个是利用替换模板中特殊字符的原理写的。供大家交流学习……
template.html 的主要代码

1<html>
2    <head>
3       <title>$htmlkey[0]</title>
4       <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
5    </head>
6    <body >
7       <table $htmlkey[1] height=\"100%\" border=\"0\" width=\"100%\" cellpadding=\"10\" cellspacing=\"0\" bgcolor=\"#eeeeee\" style=\"border:1px solid #000000\">
8         <tr>
9          <td width=\"100%\" valign=\"middle\" align=\"left\">
10            <span style=\"color: $htmlkey[2];font-size: $htmlkey[3]\">$htmlkey[4]</span>
11          </td>
12         </tr>
13       </table>
14    </body>
15</html>
16


1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using Mysqlserver;//数据库操作类
12using System.IO;
13using System.Text;
14namespace NewsAdd
15{
16  public partial class Admin_AdminPanel_NewsAdd : System.Web.UI.Page
17  {
18    protected void Page_Load(object sender, EventArgs e)
19    {
20
21    }
22    protected void Button1_Click(object sender, EventArgs e)
23    {
24      string strTitle=Request.Form[\"Title\"].ToString();
25      string strContent=Request.Form[\"Content\"].ToString();
26      SqlServerDataBase db = new SqlServerDataBase();
27      bool success = db.Insert(\"insert into inNews(Title,Content)values(&#39;\" + strTitle + \"&#39;,&#39;\" + strContent + \"&#39;)\", null);
28      //if (success)
29        // Message.Text = \"添加成功!\";
30      /**//**//**////////////////////////////创建当前日期的文件夹开始
31      string dir = Server.MapPath(\"../../\"+\"NewsFiles/\"+DateTime.Now.ToString(\"yyMMdd\"));
32      if (!Directory.Exists(dir))
33      {
34        Directory.CreateDirectory(dir);
35      }
36      /**//**//**////////////////////////////创建当前日期的文件夹结束
37      
38      string[] newContent = new string[5];//定义和html标记数目一致的数组
39      StringBuilder strhtml = new StringBuilder();
40      try
41      {
42        //创建StreamReader对象
43        using (StreamReader sr = new StreamReader(Server.MapPath(\"../../\" + \"NewsFiles/\") + \"\\template.html\"))
44        {
45          String oneline;
46          //读取指定的HTML文件模板
47          while ((oneline = sr.ReadLine()) != null)
48          {
49            strhtml.Append(oneline);
50          }
51          sr.Close();
52        }
53      }
54      catch (Exception err)
55      {
56        //输出异常信息
57        Response.Write(err.ToString());
58      }
59      //为标记数组赋值
60      newContent[0] = strTitle;//标题
61      newContent[1] = \"BackColor=&#39;#cccfff&#39;\";//背景色
62      newContent[2] = \"#ff0000\";//字体颜色
63      newContent[3] = \"100px\";//字体大小
64      newContent[4] = strContent;//主要内容
65
66      //根据上面新的内容生成html文件
67      try
68      {
69        //指定要生成的HTML文件
70        string fname = Server.MapPath(\"../../\" + \"NewsFiles/\" + DateTime.Now.ToString(\"yyMMdd\")) + \"\\\" + DateTime.Now.ToString(\"yyyymmddhhmmss\") + \".html\";
71        //替换html模版文件里的标记为新的内容
72        for (int i = 0; i < 5; i++)
73        {
74          strhtml.Replace(\"$htmlkey[\" + i + \"]\", newContent);
75        }
76        //创建文件信息对象
77        FileInfo finfo = new FileInfo(fname);
78        //以打开或者写入的形式创建文件流
79        using (FileStream fs = finfo.OpenWrite())
80        {
81          //根据上面创建的文件流创建写数据流
82          StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(\"GB2312\"));
83          //把新的内容写到创建的HTML页面中
84          sw.WriteLine(strhtml);
85          sw.Flush();
86          sw.Close();
87        }
88      }
89      catch (Exception err)
90      {
91        Response.Write(err.ToString());
92      }
93    }
94}
95}
96
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-15 21:44 , Processed in 0.107378 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表