asp.net基于替换模版页的形式生成静态页的方法
程序员文章站
2023-12-12 20:11:28
本文实例讲述了asp.net基于替换模版页的形式生成静态页的方法。分享给大家供大家参考,具体如下:
第一步:新建项目,创建一个简单模版页:templatepage.htm...
本文实例讲述了asp.net基于替换模版页的形式生成静态页的方法。分享给大家供大家参考,具体如下:
第一步:新建项目,创建一个简单模版页:templatepage.htm
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>porschev 生成静态页简单示例</title> </head> <body> <h1>$porschev[0]$</h1> <ul> <li>页标题:$porschev[0]$</li> <li>名称:$porschev[1]$</li> <li>网址:<a href="$porschev[2]$" target="_blank">$porschev[2]$</a></li> <li>时间:$porschev[3]$</li> <li>详述:$porschev[4]$</li> </ul> </body> </html>
第二步:创建一个config文件:createhtml.config
<?xml version="1.0" encoding="utf-8" ?> <web> <website key="0" value="title"/> <website key="1" value="name"/> <website key="2" value="url"/> <website key="3" value="createdate"/> <website key="4" value="desc"/> </web>
第三步:编写生成静态页代码:(添加system.web引用)
using system; using system.collections.generic; using system.linq; using system.text; using system.io; using system.xml; namespace createhtmlbll { public class createhtmlbll { #region##读取配置文件某节点的个数 ///<summary> /// 读取配置文件某节点的个数 ///</summary> ///<param name="path">配置文件的路径</param> ///<param name="nodename">要获取的节点</param> ///<returns>返回节点个数</returns> private int readconfig(string path,string nodename) { string absopath = string.empty; //绝对路径 try { absopath = system.web.httpcontext.current.server.mappath(path); xmldocument xd = new xmldocument(); xd.load(absopath); xmlnodelist nodelist = xd.selectnodes(nodename); //得到相应节点的集合 return nodelist.count; } catch (exception) { throw; } } #endregion #region##创建文件夹 ///<summary> /// 创建文件夹 ///</summary> ///<param name="path">要创建的路径</param> public void creatfolder(string path) { string absopath = string.empty; //绝对路径 try { absopath = system.web.httpcontext.current.server.mappath(path); if (!directory.exists(absopath)) { directory.createdirectory(absopath); } } catch (exception) { throw; } } #endregion #region##生成html页 ///<summary> /// 生成html页 ///</summary> ///<param name="configpath">配置文件的路径</param> ///<param name="confignodename">配置文件节点名</param> ///<param name="tempath">模版页路径</param> ///<param name="arr">替换数组</param> ///<param name="createpath">生成html路径</param> public void createhtml(string configpath, string confignodename, string tempath, string[] arr,string createpath) { string filename = string.empty; //生成文件名 string absocrepath = string.empty; //生成页绝对路径 string absotempath = string.empty; //模版页的绝对中径 int nodecount = 0; //节点数 try { absocrepath = system.web.httpcontext.current.server.mappath(createpath); absotempath = system.web.httpcontext.current.server.mappath(tempath); nodecount = readconfig(configpath, confignodename); filestream fs = file.open(absotempath, filemode.open, fileaccess.read); //读取模版页 streamreader sr = new streamreader(fs, encoding.getencoding("utf-8")); stringbuilder sb = new stringbuilder(sr.readtoend()); sr.close(); sr.dispose(); for (int i = 0; i < nodecount; i++) { sb.replace("$porschev[" + i + "]$", arr[i]); } creatfolder(createpath); filename = datetime.now.tofiletime().tostring() + ".html"; //设置文件名(这里可以根据需要变化命名) filestream cfs = file.create(absocrepath + "/" + filename); streamwriter sw = new streamwriter(cfs, encoding.getencoding("utf-8")); sw.write(sb.tostring()); sw.flush(); sw.close(); sw.dispose(); } catch (exception) { throw; } } #endregion } }
第四步:测式生成
protected void page_load(object sender, eventargs e) { createhtml(); } #region##生成静态页 ///<summary> /// 生成静态页 ///</summary> public void createhtml() { try { string[] arr = new string[5]; arr[0] = "porschev 静态页测式"; arr[1] = "dtan"; arr[2] = "www.jb51.net"; arr[3] = datetime.today.tostring(); arr[4] = "欢迎来到"; createhtmlbll.createhtmlbll chb = new createhtmlbll.createhtmlbll(); chb.createhtml("createhtml.config", "web/website","template/templatepage.htm", arr,"porschev"); } catch (exception ex) { throw ex; } } #endregion
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。