.net如何將aspx生成html(cms靜態頁面原理)
阿新 • • 發佈:2019-01-10
相信大家都遇到過把web網站生成html發不到網上.也很好奇一些cms上都有這個功能,其實很簡單
包含起來
5 2.生成頁面的ASPX中,用StreamReader讀取HTM模板,用REPLACE
6 替換$value$
7 3.把完成的字串用StreamWriter輸出
8 參考程式碼如下:
9 1)定義模板emplate.htm
10 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
11 <html xmlns="http://www.w3.org/1999/xhtml " >
12 <head>
13 <title> $title$ 生成靜態頁的Demo|-51aspx.com</title>
14 <style type="text/css">
15 <!--
16 .STYLE1 {
17 font-size: 16px;
18 font-weight: bold;
19 }
20 -->
21 </style>
22 </head>
23 <body>
24 <br />
25 <br />
26 <table width="100%" border="0" bgcolor="#339900">
27 <tr>
28 <td height="34" align="center" bgcolor="#FFFFFF"><span class="STYLE1">$title$ </span></td>
29 </tr>
30 <tr>
31 <td height="42" bgcolor="#FFFFFF"><br />
32 <br />
33 內容:$content$ </td>
34 </tr>
35 </table>
36 <a href="#" target="_blank">版權所有</a>
37 </body>
38 </html>
39 2)在Default.aspx頁面的按扭的事件處理中寫如下程式碼:
40 //原始碼是替換掉模板中的特徵字元 41 string mbPath = Server.MapPath("template.htm");
42 Encoding code = Encoding.GetEncoding("gb2312");
43 StreamReader sr = null;
44 StreamWriter sw = null;
45 string str = null;
46 //讀取 47 try
48 {
49 sr = new StreamReader(mbPath, code);
50 str = sr.ReadToEnd();
51 }
52 catch (Exception ex)
53 {
54 throw ex;
55 }
56 finally
57 {
58 sr.Close();
59 }
60 //根據時間自動重新命名,副檔名也可以自行修改 61 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
62 str = str.Replace("$title{1}quot;, txtTitle.Text);//替換Title 63 str = str.Replace("$content{1}quot;, txtContent.Text);//替換content 64 //生成靜態檔案 65 try
66 {
67 sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
68 sw.Write(str);
69 sw.Flush();
70 }
71 catch (Exception ex)
72 {
73 throw ex;
74 }
75 finally
76 {
77 sw.Close();
78 Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經生成,儲存在htm資料夾下!");
79 }
80
81
82 方法二:根據Url地址生成靜態頁保持
83 思路分析:
84 直接將做好的動態頁面翻譯成靜態頁面,所以生成的內容不夠靈活
85 參考程式碼:
86 //根據Url地址生成靜態頁保持 87 protected void Button2_Click(object sender, EventArgs e)
88 {
89 Encoding code = Encoding.GetEncoding("utf-8");
90 StreamReader sr = null;
91 StreamWriter sw = null;
92 string str = null;
93 //讀取遠端路徑 94 WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
95 WebResponse myTemp = temp.GetResponse();
96 sr = new StreamReader(myTemp.GetResponseStream(), code);
97 //讀取 98 try
99 {
100 sr = new StreamReader(myTemp.GetResponseStream(), code);
101 str = sr.ReadToEnd();
102 }
103 catch (Exception ex)
104 {
105 throw ex;
106 }
107 finally
108 {
109 sr.Close();
110 }
111 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
112 //寫入 113 try
114 {
115 sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
116 sw.Write(str);
117 sw.Flush();
118 }
119 catch (Exception ex)
120 {
121 throw ex;
122 }
123 finally
124 {
125 sw.Close();
126 Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經生成,儲存在htm資料夾下!");
127 }
128 }
大體思路就是:在你的後臺,做一html頁面生成器.點選時.迴圈web目錄下的aspx檔案(這一點應該不難實現).將他們全部生成html.這就是阻礙大家的路障.下面簡單介紹一個demo
當然也可以做的跟cms一樣,後臺視覺化管理模板頁.其原理只不過把模板html讀到編輯器裡.然後定義一些標籤來讀取資料.(本人做了一個bayyter.game的cms就是用的這種方法,每個遊戲專題只需要美工寫頁面,插入標籤就OK.個人感覺速度還很快.這個站放在國外.所以要上vpn)
1方法一:根據模板生成,保持在html資料夾中
2 思路分析:
3 1.寫一個自定義的HTM模板 其中需要替換的地方用$value$這樣
4
5 2.生成頁面的ASPX中,用StreamReader讀取HTM模板,用REPLACE
6 替換$value$
7 3.把完成的字串用StreamWriter輸出
8 參考程式碼如下:
9 1)定義模板emplate.htm
10 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
11 <html xmlns="http://www.w3.org/1999/xhtml
12 <head>
13 <title> $title$ 生成靜態頁的Demo|-51aspx.com</title>
14 <style type="text/css">
15 <!--
16 .STYLE1 {
17 font-size: 16px;
18 font-weight: bold;
19 }
20 -->
21 </style>
22 </head>
23 <body>
24 <br />
25
26 <table width="100%" border="0" bgcolor="#339900">
27 <tr>
28 <td height="34" align="center" bgcolor="#FFFFFF"><span class="STYLE1">$title$ </span></td>
29 </tr>
30 <tr>
31 <td height="42" bgcolor="#FFFFFF"><br />
32 <br />
33 內容:$content$ </td>
34 </tr>
35 </table>
36 <a href="#" target="_blank">版權所有</a>
37 </body>
38 </html>
39 2)在Default.aspx頁面的按扭的事件處理中寫如下程式碼:
40 //原始碼是替換掉模板中的特徵字元 41 string mbPath = Server.MapPath("template.htm");
42 Encoding code = Encoding.GetEncoding("gb2312");
43 StreamReader sr = null;
44 StreamWriter sw = null;
45 string str = null;
46 //讀取 47 try
48 {
49 sr = new StreamReader(mbPath, code);
50 str = sr.ReadToEnd();
51 }
52 catch (Exception ex)
53 {
54 throw ex;
55 }
56 finally
57 {
58 sr.Close();
59 }
60 //根據時間自動重新命名,副檔名也可以自行修改 61 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
62 str = str.Replace("$title{1}quot;, txtTitle.Text);//替換Title 63 str = str.Replace("$content{1}quot;, txtContent.Text);//替換content 64 //生成靜態檔案 65 try
66 {
67 sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
68 sw.Write(str);
69 sw.Flush();
70 }
71 catch (Exception ex)
72 {
73 throw ex;
74 }
75 finally
76 {
77 sw.Close();
78 Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經生成,儲存在htm資料夾下!");
79 }
80
81
82 方法二:根據Url地址生成靜態頁保持
83 思路分析:
84 直接將做好的動態頁面翻譯成靜態頁面,所以生成的內容不夠靈活
85 參考程式碼:
86 //根據Url地址生成靜態頁保持 87 protected void Button2_Click(object sender, EventArgs e)
88 {
89 Encoding code = Encoding.GetEncoding("utf-8");
90 StreamReader sr = null;
91 StreamWriter sw = null;
92 string str = null;
93 //讀取遠端路徑 94 WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
95 WebResponse myTemp = temp.GetResponse();
96 sr = new StreamReader(myTemp.GetResponseStream(), code);
97 //讀取 98 try
99 {
100 sr = new StreamReader(myTemp.GetResponseStream(), code);
101 str = sr.ReadToEnd();
102 }
103 catch (Exception ex)
104 {
105 throw ex;
106 }
107 finally
108 {
109 sr.Close();
110 }
111 string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
112 //寫入 113 try
114 {
115 sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
116 sw.Write(str);
117 sw.Flush();
118 }
119 catch (Exception ex)
120 {
121 throw ex;
122 }
123 finally
124 {
125 sw.Close();
126 Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經生成,儲存在htm資料夾下!");
127 }
128 }