asp.net基于替换模版页的形式生成静态页的方法
本文实例讲述了asp.net基于替换模版页的形式生成静态页的方法。分享给大家供大家参考,具体如下:
第一步:新建项目,创建一个简单模版页:TemplatePage.htm
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="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>网址:<ahref="$Porschev[2]$"target="_blank">$Porschev[2]$</a></li> <li>时间:$Porschev[3]$</li> <li>详述:$Porschev[4]$</li> </ul> </body> </html>
第二步:创建一个config文件:CreateHtml.config
<?xmlversion="1.0"encoding="utf-8"?> <web> <websitekey="0"value="title"/> <websitekey="1"value="name"/> <websitekey="2"value="url"/> <websitekey="3"value="createDate"/> <websitekey="4"value="desc"/> </web>
第三步:编写生成静态页代码:(添加System.Web引用)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Xml;
namespaceCreateHtmlBLL
{
publicclassCreateHtmlBLL
{
#region##读取配置文件某节点的个数
///<summary>
///读取配置文件某节点的个数
///</summary>
///<paramname="path">配置文件的路径</param>
///<paramname="nodeName">要获取的节点</param>
///<returns>返回节点个数</returns>
privateintReadConfig(stringpath,stringnodeName)
{
stringabsoPath=string.Empty;//绝对路径
try
{
absoPath=System.Web.HttpContext.Current.Server.MapPath(path);
XmlDocumentxd=newXmlDocument();
xd.Load(absoPath);
XmlNodeListnodeList=xd.SelectNodes(nodeName);//得到相应节点的集合
returnnodeList.Count;
}
catch(Exception)
{
throw;
}
}
#endregion
#region##创建文件夹
///<summary>
///创建文件夹
///</summary>
///<paramname="path">要创建的路径</param>
publicvoidCreatFolder(stringpath)
{
stringabsoPath=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>
///<paramname="configPath">配置文件的路径</param>
///<paramname="configNodeName">配置文件节点名</param>
///<paramname="temPath">模版页路径</param>
///<paramname="arr">替换数组</param>
///<paramname="createPath">生成HTML路径</param>
publicvoidCreateHtml(stringconfigPath,StringconfigNodeName,stringtemPath,string[]arr,stringcreatePath)
{
stringfileName=string.Empty;//生成文件名
stringabsoCrePath=string.Empty;//生成页绝对路径
stringabsoTemPath=string.Empty;//模版页的绝对中径
intnodeCount=0;//节点数
try
{
absoCrePath=System.Web.HttpContext.Current.Server.MapPath(createPath);
absoTemPath=System.Web.HttpContext.Current.Server.MapPath(temPath);
nodeCount=ReadConfig(configPath,configNodeName);
FileStreamfs=File.Open(absoTemPath,FileMode.Open,FileAccess.Read);//读取模版页
StreamReadersr=newStreamReader(fs,Encoding.GetEncoding("utf-8"));
StringBuildersb=newStringBuilder(sr.ReadToEnd());
sr.Close();
sr.Dispose();
for(inti=0;i<nodeCount;i++)
{
sb.Replace("$Porschev["+i+"]$",arr[i]);
}
CreatFolder(createPath);
fileName=DateTime.Now.ToFileTime().ToString()+".html";//设置文件名(这里可以根据需要变化命名)
FileStreamcfs=File.Create(absoCrePath+"/"+fileName);
StreamWritersw=newStreamWriter(cfs,Encoding.GetEncoding("utf-8"));
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
sw.Dispose();
}
catch(Exception)
{
throw;
}
}
#endregion
}
}
第四步:测式生成
protectedvoidPage_Load(objectsender,EventArgse)
{
CreateHtml();
}
#region##生成静态页
///<summary>
///生成静态页
///</summary>
publicvoidCreateHtml()
{
try
{
string[]arr=newstring[5];
arr[0]="Porschev静态页测式";
arr[1]="dtan";
arr[2]="www.nhooo.com";
arr[3]=DateTime.Today.ToString();
arr[4]="欢迎来到毛票票";
CreateHtmlBLL.CreateHtmlBLLchb=newCreateHtmlBLL.CreateHtmlBLL();
chb.CreateHtml("CreateHtml.config","web/website","Template/TemplatePage.htm",arr,"Porschev");
}
catch(Exceptionex)
{
throwex;
}
}
#endregion
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.netajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。