当前位置:Gxlcms > asp.net > asp.net基于替换模版页的形式生成静态页的方法

asp.net基于替换模版页的形式生成静态页的方法

时间:2021-07-01 10:21:17 帮助过:8人阅读

本文实例讲述了asp.net基于替换模版页的形式生成静态页的方法。分享给大家供大家参考,具体如下:

第一步:新建项目,创建一个简单模版页:TemplatePage.htm

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Porschev 生成静态页简单示例</title>
  6. </head>
  7. <body>
  8. <h1>$Porschev[0]$</h1>
  9. <ul>
  10. <li>页标题:$Porschev[0]$</li>
  11. <li>名称:$Porschev[1]$</li>
  12. <li>网址:<a href="$Porschev[2]$" target="_blank">$Porschev[2]$</a></li>
  13. <li>时间:$Porschev[3]$</li>
  14. <li>详述:$Porschev[4]$</li>
  15. </ul>
  16. </body>
  17. </html>

第二步:创建一个config文件:CreateHtml.config

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <web>
  3. <website key="0" value="title"/>
  4. <website key="1" value="name"/>
  5. <website key="2" value="url"/>
  6. <website key="3" value="createDate"/>
  7. <website key="4" value="desc"/>
  8. </web>

第三步:编写生成静态页代码:(添加System.Web引用)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. namespace CreateHtmlBLL
  8. {
  9. public class CreateHtmlBLL
  10. {
  11. #region##读取配置文件某节点的个数
  12. ///<summary>
  13. /// 读取配置文件某节点的个数
  14. ///</summary>
  15. ///<param name="path">配置文件的路径</param>
  16. ///<param name="nodeName">要获取的节点</param>
  17. ///<returns>返回节点个数</returns>
  18. private int ReadConfig(string path,string nodeName)
  19. {
  20. string absoPath = string.Empty; //绝对路径
  21. try
  22. {
  23. absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
  24. XmlDocument xd = new XmlDocument();
  25. xd.Load(absoPath);
  26. XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相应节点的集合
  27. return nodeList.Count;
  28. }
  29. catch (Exception)
  30. {
  31. throw;
  32. }
  33. }
  34. #endregion
  35. #region##创建文件夹
  36. ///<summary>
  37. /// 创建文件夹
  38. ///</summary>
  39. ///<param name="path">要创建的路径</param>
  40. public void CreatFolder(string path)
  41. {
  42. string absoPath = string.Empty; //绝对路径
  43. try
  44. {
  45. absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
  46. if (!Directory.Exists(absoPath))
  47. {
  48. Directory.CreateDirectory(absoPath);
  49. }
  50. }
  51. catch (Exception)
  52. {
  53. throw;
  54. }
  55. }
  56. #endregion
  57. #region##生成HTML页
  58. ///<summary>
  59. /// 生成HTML页
  60. ///</summary>
  61. ///<param name="configPath">配置文件的路径</param>
  62. ///<param name="configNodeName">配置文件节点名</param>
  63. ///<param name="temPath">模版页路径</param>
  64. ///<param name="arr">替换数组</param>
  65. ///<param name="createPath">生成HTML路径</param>
  66. public void CreateHtml(string configPath, String configNodeName, string temPath, string[] arr,string createPath)
  67. {
  68. string fileName = string.Empty; //生成文件名
  69. string absoCrePath = string.Empty; //生成页绝对路径
  70. string absoTemPath = string.Empty; //模版页的绝对中径
  71. int nodeCount = 0; //节点数
  72. try
  73. {
  74. absoCrePath = System.Web.HttpContext.Current.Server.MapPath(createPath);
  75. absoTemPath = System.Web.HttpContext.Current.Server.MapPath(temPath);
  76. nodeCount = ReadConfig(configPath, configNodeName);
  77. FileStream fs = File.Open(absoTemPath, FileMode.Open, FileAccess.Read); //读取模版页
  78. StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
  79. StringBuilder sb = new StringBuilder(sr.ReadToEnd());
  80. sr.Close();
  81. sr.Dispose();
  82. for (int i = 0; i < nodeCount; i++)
  83. {
  84. sb.Replace("$Porschev[" + i + "]$", arr[i]);
  85. }
  86. CreatFolder(createPath);
  87. fileName = DateTime.Now.ToFileTime().ToString() + ".html"; //设置文件名(这里可以根据需要变化命名)
  88. FileStream cfs = File.Create(absoCrePath + "/" + fileName);
  89. StreamWriter sw = new StreamWriter(cfs, Encoding.GetEncoding("utf-8"));
  90. sw.Write(sb.ToString());
  91. sw.Flush();
  92. sw.Close();
  93. sw.Dispose();
  94. }
  95. catch (Exception)
  96. {
  97. throw;
  98. }
  99. }
  100. #endregion
  101. }
  102. }

第四步:测式生成

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. CreateHtml();
  4. }
  5. #region##生成静态页
  6. ///<summary>
  7. /// 生成静态页
  8. ///</summary>
  9. public void CreateHtml()
  10. {
  11. try
  12. {
  13. string[] arr = new string[5];
  14. arr[0] = "Porschev 静态页测式";
  15. arr[1] = "dtan";
  16. arr[2] = "www.gxlcms.com";
  17. arr[3] = DateTime.Today.ToString();
  18. arr[4] = "欢迎来到脚本之家";
  19. CreateHtmlBLL.CreateHtmlBLL chb = new CreateHtmlBLL.CreateHtmlBLL();
  20. chb.CreateHtml("CreateHtml.config", "web/website","Template/TemplatePage.htm", arr,"Porschev");
  21. }
  22. catch (Exception ex)
  23. {
  24. throw ex;
  25. }
  26. }
  27. #endregion

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

人气教程排行