【转】ASP.NET 生成HTML静态页面实例

转载自:http://blog.baigoocms.com/?p=100

1 配置WEB.CONFIG

复制XML代码保存代码

2.创建模板页

阅读代码编辑代码运行效果复制HTML代码保存代码

生成时间:my_time

作者: my_name

替换的正文:my_body

[ DllImport( "kernel32", EntryPoint="GetVersionEx" )]

DllImportAttribute特性的公共字段如下: 1、CallingConvention 指示向非托管实现传递方法参数时所用的

CallingConvention 值。 CallingConvention.Cdecl : 调用方清理堆栈。它使您能够调用具有 varargs 的函数。

CallingConvention.StdCall : 被调用方清理堆栈。它是从托管代码调用非托管函数的默认约定。 2、CharSet

控制调用函数的名称版本及指示如何向方法封送 String 参数。 此字段被设置为 CharSet 值之一。如果 CharSet 字段设置为

Unicode,则所有字符串参数在传递到非托管实现之前都转换成 Unicode 字符。

生成时间:my_time

作者: my_name

替换的正文:my_body

[ DllImport( "kernel32", EntryPoint="GetVersionEx" )]

DllImportAttribute特性的公共字段如下: 1、CallingConvention 指示向非托管实现传递方法参数时所用的

CallingConvention 值。 CallingConvention.Cdecl : 调用方清理堆栈。它使您能够调用具有 varargs 的函数。

CallingConvention.StdCall : 被调用方清理堆栈。它是从托管代码调用非托管函数的默认约定。 2、CharSet

控制调用函数的名称版本及指示如何向方法封送 String 参数。 此字段被设置为 CharSet 值之一。如果 CharSet 字段设置为

Unicode,则所有字符串参数在传递到非托管实现之前都转换成 Unicode 字符。

3. 写静态页类

View Code
1 using System;
2
3 using System.IO;
4
5 namespace Junval.createHtm
6
7 {
8
9 ///
10
11 /// ExcetueHtm 的摘要说明。
12
13 ///
14
15 public class ExcetueHtm
16
17 {
18
19 private string sId; //需要生成静态页的数据ID
20
21 private string sTemp; //需要生成静态页的模板名称
22
23 public ExcetueHtm()
24
25 {
26
27 //
28
29 // TOD 在此处添加构造函数逻辑
30
31 //
32
33 }
34
35 ///
36
37 // 设置ID属性
38
39 ///
40
41 public string ID
42
43 {
44
45 get { return sId; }
46
47 set { sId = value; }
48
49 }
50
51 public string Temp
52
53 {
54
55 get { return sTemp; }
56
57 set { sTemp = value; }
58
59 }
60
61 ///
62
63 /// 生成静态页面
64
65 ///
66
67 ///
68
69 public bool CreateHtml()
70
71 {
72
73 //存放HTML路径
74
75 string ls_path = System.Configuration.ConfigurationSettings.AppSettings["htmlPath"].ToString();
76
77 //选择模板
78
79 string ls_temp = ls_path + sTemp;
80
81 System.IO.StreamReader Sr = null;
82
83 System.IO.StreamWriter Sw = null;
84
85 string str = “”;
86
87 //读模板
88
89 try
90
91 {
92
93 Sr = new StreamReader(ls_temp, System.Text.Encoding.GetEncoding(”GB2312″));
94
95 str = Sr.ReadToEnd();
96
97 }
98
99 catch (Exception ex)
100
101 {
102
103 System.Web.HttpContext.Current.Response.Write(ex.Message.ToString());
104
105 System.Web.HttpContext.Current.Response.End();
106
107 }
108
109 finally
110
111 {
112
113 Sr.Close();
114
115 }
116
117 string sFilename = sId.ToString() + “.htm”;
118
119 //替换模板内容
120
121 str = ReplaceStr(str);
122
123 //写vhtml
124
125 try
126
127 {
128
129 Sw = new StreamWriter(ls_path + sFilename, false, System.Text.Encoding.GetEncoding(”gb2312″));
130
131 Sw.Write(str);
132
133 Sw.Flush();
134
135 }
136
137 catch (Exception ex)
138
139 {
140
141 System.Web.HttpContext.Current.Response.Write(ex.Message.ToString());
142
143 System.Web.HttpContext.Current.Response.End();
144
145 }
146
147 finally
148
149 {
150
151 Sw.Close();
152
153 }
154
155 return true;
156
157 }
158
159 private string ReplaceStr(string ls_str)
160
161 {
162
163 //根据模板 选择不通的替换函数
164
165 return TempNo_1(ls_str).ToString();
166
167 }
168
169 //NO1模板替换方案
170
171 private string TempNo_1(string ls_str)
172
173 {
174
175 string ls_Tilte = “标题:一号模板替换方案”;
176
177 string ls_time = DateTime.Now.ToString();
178
179 ls_str = ls_str.Replace(”my_title”, ls_Tilte);
180
181 ls_str = ls_str.Replace(”my_time”, ls_time);
182
183 ls_str = ls_str.Replace(”my_name”, “Junval Shi”);
184
185 ls_str = ls_str.Replace(”my_body”, ” ......”);
186
187 return ls_str;
188
189 }
190
191 ///
192
193 /// 生成静态页面
194
195 /// ///需要生成静态页的数据ID ///需要生成静态页的模板名称 ///
196
197 public bool CreateHtml(string pid, string ptemp)
198
199 {
200
201 return true;
202
203 }
204
205 }
206
207 }
208
209 <%@ Page language=”c#” Codebehind=”Main.aspx.cs” AutoEventWireup=false” Inherits=”Junval.createHtm.Main” %>
210
211 <%@ Page language=”c#” Codebehind=”Main.aspx.cs” AutoEventWireup=false” Inherits=”Junval.createHtm.Main” %>
212
213 CS:
214
215 using System;
216
217 using System.Collections;
218
219 using System.ComponentModel;
220
221 using System.Data;
222
223 using System.Drawing;
224
225 using System.Web;
226
227 using System.Web.SessionState;
228
229 using System.Web.UI;
230
231 using System.Web.UI.WebControls;
232
233 using System.Web.UI.HtmlControls;
234
235 namespace Junval.createHtm
236
237 {
238
239 ///
240
241 /// Main 的摘要说明。
242
243 ///
244
245 public class Main : System.Web.UI.Page
246
247 {
248
249 protected System.Web.UI.WebControls.TextBox TextBox1;
250
251 protected System.Web.UI.WebControls.Button CreateHtml;
252
253 private void Page_Load(object sender, System.EventArgs e)
254
255 {
256
257 // 在此处放置用户代码以初始化页面
258
259 }
260
261 #region Web 窗体设计器生成的代码
262
263 override protected void OnInit(EventArgs e)
264
265 {
266
267 //
268
269 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
270
271 //
272
273 InitializeComponent();
274
275 base.OnInit(e);
276
277 }
278
279 ///
280
281 /// 设计器支持所需的方法 – 不要使用代码编辑器修改
282
283 /// 此方法的内容。
284
285 ///
286
287 private void InitializeComponent()
288
289 {
290
291 this.CreateHtml.Click += new System.EventHandler(this.CreateHtml_Click);
292
293 this.Load += new System.EventHandler(this.Page_Load);
294
295 }
296
297 #endregion
298
299 private void CreateHtml_Click(object sender, System.EventArgs e)
300
301 {
302
303 string ls_id = TextBox1.Text.Trim();
304
305 ExcetueHtm CH = new ExcetueHtm();
306
307 CH.ID = ls_id;
308
309 CH.Temp = “HTMLPage1.html”;
310
311 if (CH.CreateHtml())
312
313 {
314
315 Response.Write(”");
316
317 }
318
319 else
320
321 {
322
323 Response.Write(”ErrEs”);
324
325 }
326
327 }
328
329 }
330
331 }

posted @ 2011-06-12 10:48  Credo Chen  阅读(237)  评论(0编辑  收藏  举报
无觅相关文章插件,快速提升流量