亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > HTML > 正文

根據html頁面模板動態生成html頁面(c#類)

2024-08-26 00:15:37
字體:
來源:轉載
供稿:網友

一直以為動態生成靜態頁面不好做,昨天在網上找了下,我暈,其實很簡單,思路大概是這樣的,
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);
192            }
193        }
194        /**//// <summary>
195        /// 開始進行aspxtohtml轉換
196        /// </summary>
197        /// <returns>返回值為成功創建后的文件名稱</returns>
198        /// <remarks>在調用此方法之前必需確定已調用settemplatefileparameter 和setaspxfileparameter方法進行相應的付值操作</remarks>
199        public string startconvert()
200        {
201            if(this._templatefileparameter.length==this._aspxfileparameter.length)
202            {
203                return writefile();
204            }
205            else{
206                return null;
207            }
208        }
209        /**//// <summary>
210        /// 開始進行aspxtohtml轉換
211        /// </summary>
212        /// <param name="htmlparam">html模板頁中的所有參數數組</param>
213        /// <param name="aspxparam">aspx頁面中要代替html模板頁中參數值數組</param>
214        /// <returns>返回值為成功創建后的文件名稱</returns>
215        public string startconvert(string[] htmlparam,string[] aspxparam)
216        {
217            //先調用settemplatefileparameter 和setaspxfileparameter方法,進行付值操作
218            settemplatefileparameter(htmlparam);
219            setaspxfileparameter(aspxparam);
220            //
221            string fn=this.startconvert();
222            //
223            _convertedfilename=fn;
224            //
225            return fn;
226        }
227       
228        /**//// <summary>
229        /// 用時間加隨機數生成一個文件名
230        /// </summary>
231        /// <returns></returns>
232        private string getfilename()
233        {
234            //用時間加隨機數生成一個文件名
235            system.threading.thread.sleep(50);
236            string yearstr = system.datetime.now.year.tostring();
237            string monthstr = string.format("{0:0#}",system.datetime.now.month);
238            string daystr = string.format("{0:0#}",system.datetime.now.day);
239            string hourstr = string.format("{0:0#}",system.datetime.now.hour);
240            string minutestr = string.format("{0:0#}",system.datetime.now.minute);
241            string secondstr = string.format("{0:0#}",system.datetime.now.second);
242            string millisecondstr = string.format("{0:000#}",system.datetime.now.millisecond);                   
243            system.random rd = new system.random();
244            return yearstr + monthstr + daystr + hourstr + minutestr + secondstr + millisecondstr + string.format("{0:0000#}",rd.next(100))+".html";
245            //return datetime.now.tostring("yyyymmddhhmmss")+".html";
246        }
247        /**//// <summary>
248        /// 進行轉換處理
249        /// </summary>
250        /// <returns>返回以時間命名的文件名</returns>
251        private string writefile()
252        {
253           
254            // 讀取模板文件
255            string temp = httpcontext.current.server.mappath(this.templatefilepath);
256            streamreader sr=null;           
257            string str="";
258            try
259            {
260                sr = new streamreader(temp, this.templatehtmlcode);
261                str = sr.readtoend(); // 讀取文件
262            }
263            catch(exception ex)
264            {
265                //httpcontext.current.response.write(exp.message);
266                //httpcontext.current.response.end();       
267                writeerrfile(ex);
268            }
269            finally
270            {
271                sr.close();
272            }           
273            // 替換內容
274            // 這時,模板文件已經讀入到名稱為str的變量中了
275            for(int i=0;i<this.templateparamcount;i++)
276            {
277                str =str.replace(this._templatefileparameter[i],this._aspxfileparameter[i]);
278            }       
279
280            return savefile(str);
281        }
282
283        /**//// <summary>
284        ///
285        /// </summary>
286        /// <param name="str"></param>
287        /// <returns></returns>
288
289        private string savefile(string str)
290        {
291            // 寫文件
292            streamwriter sw=null;
293            try
294            {
295               
296                string path = httpcontext.current.server.mappath(this.htmlfilepath);
297                //html文件名稱   
298                string htmlfilename=getfilename();
299                sw = new streamwriter(path + htmlfilename , false, this.code);
300                sw.write(str);
301                sw.flush();
302                return htmlfilename;
303            }
304            catch(exception ex)
305            {               
306                writeerrfile(ex);
307            }
308            finally
309            {
310                sw.close();
311            }
312            return "";
313        }
314
315        /**//// <summary>
316        /// 傳入url返回網頁的html代碼
317        /// </summary>
318        /// <param name="url">url</param>
319        /// <returns></returns>
320        public string geturltohtml(string url)
321        {           
322            try
323            {
324                system.net.webrequest wreq = system.net.webrequest.create(url);           
325                system.net.webresponse wresp =wreq.getresponse();               
326                system.io.stream respstream  = wresp.getresponsestream();               
327                system.io.streamreader reader = new system.io.streamreader(respstream, system.text.encoding.getencoding("gb2312"));
328                return  savefile(reader.readtoend());
329
330            }
331            catch(system.exception ex)
332            {
333                writeerrfile(ex);
334            }
335            return "";
336        }
337        #endregion
338
339
340        構造#region 構造       
341       
342        public aspxtohtml()
343        {
344            //
345            // todo: 在此處添加構造函數邏輯
346            //           
347        }
348
349        private void settemplateparamcount(int templateparamcount)
350        {
351            if (templateparamcount>0)
352                this.templateparamcount=templateparamcount;
353        }
354        /**//// <summary>
355        /// 提供欲代替的參數個數
356        /// </summary>
357        /// <param name="templateparamcount"></param>
358        public aspxtohtml(int templateparamcount)
359        {   
360            settemplateparamcount(templateparamcount);
361           
362        }
363        /**//// <summary>
364        ///
365        /// </summary>
366        /// <param name="templateparamcount">html模板頁中的參數個數</param>
367        /// <param name="htmlfilepath">生成的html文件所存放的文件夾路徑</param>
368        /// <param name="templatefilepath">html模板頁路徑</param>
369        public aspxtohtml(int templateparamcount,string htmlfilepath,string templatefilepath)
370        {
371            settemplateparamcount(templateparamcount);
372            this.htmlfilepath        =    htmlfilepath;
373            this.templatefilepath    =    templatefilepath;
374           
375        }
376        #endregion
377
378        #region
379       
380        /**//// <summary>
381        /// 把錯誤寫入文件方法#region 把錯誤寫入文件方法
382        /// </summary>
383        /// <param name="ee"></param>
384        private  void writeerrfile(exception ee)
385        {
386           
387            filestream fs1 = new filestream(httpcontext.current.server.mappath(errlogpath), system.io.filemode.append);
388            streamwriter sw1 = new streamwriter(fs1);
389            sw1.writeline("**************************************************");
390            sw1.writeline("錯誤日期:" + system.datetime.now);
391            sw1.writeline("錯誤描述:" + ee.message);
392            sw1.writeline("錯誤名稱:" + ee.source);
393            sw1.writeline("詳細:" + ee.tostring());
394            sw1.writeline("*************************************************");
395            sw1.close();
396        }
397        #endregion
398    }
399}
400

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产一区二区三区毛片| 精品自在线视频| 97碰碰碰免费色视频| 国产成人精品一区二区在线| 国产欧美日韩精品丝袜高跟鞋| 久久久久久久久久婷婷| 91精品久久久久久久久久久久久久| 亚洲国产一区二区三区在线观看| 91chinesevideo永久地址| 日韩在线观看成人| 午夜精品久久久久久久99热浪潮| 久久九九热免费视频| www亚洲精品| 国产91露脸中文字幕在线| 精品国产一区二区三区久久狼黑人| 国产日产久久高清欧美一区| 精品动漫一区二区三区| 欧美另类69精品久久久久9999| 亚洲综合在线播放| 91老司机在线| 精品久久久久久电影| 国产精品夜色7777狼人| 欧美激情在线有限公司| 国产精品久久一区主播| 国产精品白嫩美女在线观看| 久久99精品久久久久久琪琪| 国产91网红主播在线观看| 亚洲一区中文字幕在线观看| 国产伦精品免费视频| 在线观看国产精品91| 亚洲系列中文字幕| 精品一区二区三区电影| 亚洲国产精品va在线看黑人动漫| 国产v综合v亚洲欧美久久| 欧美日在线观看| 国产一区二区av| 97精品国产97久久久久久| 亚洲美女中文字幕| 亚洲天堂一区二区三区| 琪琪亚洲精品午夜在线| 欧美日韩国内自拍| 日韩中文字幕在线免费观看| 国产一区二中文字幕在线看| 97免费中文视频在线观看| 欧美一区二区三区……| 91免费视频国产| 亚洲精品aⅴ中文字幕乱码| 亚洲欧美综合图区| 性欧美长视频免费观看不卡| 4388成人网| 亚洲娇小xxxx欧美娇小| 欧美日本啪啪无遮挡网站| 亚洲情综合五月天| 欧美日韩一二三四五区| 欧美国产日产韩国视频| 欧美老少做受xxxx高潮| 欧美大秀在线观看| 91香蕉国产在线观看| 日本精品一区二区三区在线播放视频| 51色欧美片视频在线观看| 97久久精品人搡人人玩| 日韩精品免费看| 欧美日韩国内自拍| 亚洲va欧美va国产综合久久| 亚洲精品一二区| 91tv亚洲精品香蕉国产一区7ujn| 欧美专区日韩视频| 国产精品视频在线播放| 久久深夜福利免费观看| 亚洲精品在线视频| 亚洲色图第三页| 日韩一区二区久久久| 久久夜精品va视频免费观看| 日本亚洲欧美成人| 亚洲精品久久久久中文字幕欢迎你| 欧美成人亚洲成人| 日韩美女写真福利在线观看| 黑人巨大精品欧美一区二区免费| 精品在线欧美视频| 精品国产一区二区三区久久狼黑人| 欧美多人爱爱视频网站| 国产成人av网址| 国产精品免费久久久久久| 亚洲成人免费在线视频| 日韩在线视频观看| 蜜月aⅴ免费一区二区三区| 精品久久香蕉国产线看观看gif| 国产精品免费一区| 91精品国产网站| 中文字幕欧美视频在线| 欧美乱大交xxxxx| 一本一道久久a久久精品逆3p| 久久人人爽亚洲精品天堂| 日韩欧美福利视频| 亚洲人成在线观看| 国产美女高潮久久白浆| 97超级碰碰碰久久久| 国产精品日韩一区| 成人一区二区电影| 91精品视频观看| 97av在线视频免费播放| 国产做受高潮69| 日韩精品免费观看| 国产精欧美一区二区三区| 欧美精品做受xxx性少妇| 久久精品视频在线| 欧美亚洲视频一区二区| 成人精品久久一区二区三区| 亚洲一区999| 最近2019中文字幕第三页视频| 成人免费视频网| 亚洲韩国欧洲国产日产av| 亚洲欧美日韩一区二区三区在线| 欧美性开放视频| 欧美福利视频在线| 国外成人性视频| 日韩69视频在线观看| 亚洲精品视频中文字幕| 国产精品亚洲第一区| 国产第一区电影| 91国内精品久久| 5566日本婷婷色中文字幕97| 日韩中文在线观看| 日本久久久久亚洲中字幕| 亚洲人成网站在线播| 欧美做受高潮电影o| 国产精品女主播| 在线观看欧美www| 亚洲国产成人精品久久| 成人午夜激情网| 欧美另类暴力丝袜| 亚洲视频日韩精品| 亚洲国语精品自产拍在线观看| 国产丝袜一区视频在线观看| 欧美激情中文字幕在线| 成人激情视频在线观看| 久久色免费在线视频| 91在线中文字幕| 亚洲国产日韩精品在线| 北条麻妃在线一区二区| 性欧美激情精品| 欧美日韩国产精品专区| 按摩亚洲人久久| 欧美成人亚洲成人日韩成人| 欧美日韩性视频| 日本在线观看天堂男亚洲| 国产精品黄色影片导航在线观看| 亚洲天堂影视av| 蜜臀久久99精品久久久无需会员| 成人精品福利视频| 欧美在线亚洲一区| 日韩欧美精品网址| 国内成人精品视频| 欧美大码xxxx| 久久青草福利网站| 国产成人精品电影久久久| 欧美激情综合色综合啪啪五月| 欧美影院久久久| 国产中文日韩欧美| 日本高清+成人网在线观看| 国产欧美在线视频| 久久视频精品在线| 97精品久久久中文字幕免费| 国产美女久久精品|