?? 根據html頁面模板動態生成html頁面(c#類).txt
字號:
根據html頁面模板動態生成html頁面(c#類)
發布作者:佚名 發布時間:2006-9-9 被點擊48次
--------------------------------------------------------------------------------
一直以為動態生成靜態頁面不好做,昨天在網上找了下,我暈,其實很簡單,思路大概是這樣的,
1:建立一個html頁面模板,在這個頁面中把你想要動態顯示的地方用特殊的字符串表示(如
$htmlstrstr$);
2:在程序中用將這個html頁面讀到一個字符串變量如str;
3:用字符串的resplace方法將在第一步中特殊字符替換成你想要的內容;
4保存;
OK,so easy,今天就用C#寫了一個這樣的類,用來處理動態生成html頁面的,自認為還寫的完
整,剛接觸.NET不久,望指教
注:此類中的代碼不全是原創,部份代碼參照網友的代碼!
以下是轉換類的代碼
代碼
1using System;
2using System.Text;
3using System.Web;
4using System.Configuration;
5using System.IO;
6namespace solucky
7{
8 /**//// <summary>
9 /// AspxToHtml 的摘要說明。
10 /// 注:使用此類,你可以在web.config文件對模板類進行配置.如下
11 /**//*<appSettings>
12 <add key="templateFilePath" value="htmlmoudel.htm" />
13 <add key="htmlFilePath" value="new/"></add>
14 <add key="ErrLogPath" value="aspxTohtml_log.txt"></add>
15 </appSettings>*/
16 /**//// </summary>
17 public class AspxToHtml
18 {
19 /**//// <summary>
20 /// 模板文件中要替代的參數個數
21 /// </summary>
22 private int _templateParamCount=0;
23 /**//// <summary>
24 /// 模板文件所在的路徑
25 /// </summary>
26 private string _templateFilePath
=ConfigurationSettings.AppSettings["templateFilePath"];
27 /**//// <summary>
28 /// 轉換后的html文件所存放的路徑
29 /// </summary>
30 private string _htmlFilePath
=ConfigurationSettings.AppSettings["htmlFilePath"];
31
32 /**//// <summary>
33 /// 模板頁頁面編碼
34 /// </summary>
35 private Encoding _templateHtmlCode
=Encoding.GetEncoding("gb2312");
36
37 /**//// <summary>
38 /// 轉換后的文件編碼
39 /// </summary>
40 private Encoding _code = Encoding.GetEncoding("gb2312");
41
42 /**//// <summary>
43 /// 轉換后的html文件名
44 /// </summary>
45 private string _convertedFilename="";
46 /**//// <summary>
47 /// 模板文件中的參數
48 /// </summary>
49 private string[] _templateFileparameter ;
50
51 /**//// <summary>
52 /// aspx文件中的要代替HTML文件中的參數實際值
53 /// </summary>
54 private string[] _aspxFileparameter;
55
56 private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"];
57
58 屬性#region 屬性
59
60 /**//// <summary>
61 /// 模板文件中要替代的參數個數
62 /// </summary>
63 public int TemplateParamCount
64 {
65 get
66 {
67 return this._templateParamCount;
68 }
69 set//分配參數個數時,同時為模板文件中的參數和aspx文件中的要代替
HTML文件中的參數實際值這兩個分配實際數組
70 {
71 if (value < 0)
72 throw new ArgumentException();
73
74 if(value>0)
75 {
76 this._templateParamCount=value;
77 //模板文件中的參數
78 _templateFileparameter = new string[value];
79 //aspx文件中的要代替HTML文件中的參數實際值
80 _aspxFileparameter = new string[value];
81 }
82 else
83 this._templateParamCount=0;
84 }
85 }
86
87 /**//// <summary>
88 /// 模板文件所在的路徑
89 ///
90 /// </summary>
91 public string TemplateFilePath
92 {
93 get{ return this._templateFilePath;}
94 set{ this._templateFilePath=value;}
95 }
96 /**//// <summary>
97 /// 轉換后的html文件所存放的路徑
98 /// </summary>
99 public string HtmlFilePath
100 {
101 get{ return this._htmlFilePath;}
102 set{ this._htmlFilePath=value;}
103 }
104
105 /**//// <summary>
106 /// html模板文件編碼
107 /// </summary>
108 public Encoding TemplateHtmlCode
109 {
110 get{ return this._templateHtmlCode;}
111 set{ this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}
112 }
113 /**//// <summary>
114 /// 編碼
115 /// </summary>
116 public Encoding Code
117 {
118 get{ return this._code;}
119 set{ this._code=Encoding.GetEncoding(value.ToString());}
120 }
121 /**//// <summary>
122 /// 錯誤文件所在路徑
123 /// </summary>
124 public string ErrLogPath
125 {
126 get{
127 if(!(this._errlogPath==null))
128 return this._errlogPath;
129 else
130 return "aspxTohtml_log.txt";
131 }
132 set{this._errlogPath=value;}
133 }
134
135
136 #endregion
137
138 操作#region 操作
139
140 /**//// <summary>
141 /// 獲取轉換后的html文件所在相對文件路徑
142 /// 如:如果HtmlFilePath="/news/"
143 /// 轉換后的html文件名為200505050505.html
144 /// 則返回的值為/news/200505050505.html
145 /// </summary>
146 /// <remarks>如果在未調用StartConvert方法之前調用此屬性則返回
null</remarks>
147 public string HtmlFileVirtualPath
148 {
149 get
150 {
151 if(!(this._convertedFilename==""))
152 return this.HtmlFilePath+this._convertedFilename;
153 else
154 return null;
155 }
156 }
157
158 /**//// <summary>
159 /// 為HTML頁面參數數組付值
160 /// </summary>
161 /// <param name="param"></param>
162 public void setTemplateFileparameter(string[] param)
163 {
164 try
165 {
166 if(param.Length==this.TemplateParamCount)
167 this._templateFileparameter=param;
168 //else//與原定義的個數不等
169 //
170 }
171 catch(System.Exception ex)
172 {
173 WriteErrFile(ex);
174 }
175 }
176 /**//// <summary>
177 /// 為aspx文件中將要替換html文件中的參數數組付值
178 /// </summary>
179 /// <param name="param"></param>
180 public void setAspxFileparameter(string[] param)
181 {
182 try
183 {
184 if(param.Length==this.TemplateParamCount)
185 this._aspxFileparameter=param;
186 //else//與原定義的個數不等
187 //
188 }
189 catch(System.Exception ex)
190 {
191 WriteErrFile(ex);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -