性能优化——生成静态页面(二)
当整个页面静态化的时候可以使用流的方式,但是如果局部页面生成静态页面的话就可以使用模板的形式了。
使用模板页面生成静态页面思路:使用DW之类的工具生成一个静态页面模板。读取模板,然后对立面的特殊标记使用真实的数据替换掉,并生成一个html文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; namespace ConsoleApplication1 { public abstract class ConvertToHtml { private string fileName; private string savePath; public string FileName { set; get; } public string SavePath { set; get; } /// <summary> /// 生成静态页面抽象方法 /// </summary> /// <param name="param"></param> /// <returns></returns> public abstract bool CreateHtml(object param); /// <summary> /// 读取配置文件 /// </summary> /// <param name="dirName">配置文件路径名</param> /// <param name="tag">配置文件中的表签名</param> /// <returns></returns> private int GetConfig(string dirName, string tag) { XmlDataDocument config = new XmlDataDocument(); try { config.Load(dirName); } catch (Exception ex) { throw ex; } XmlNodeList list = config.GetElementsByTagName(tag); return list.Count; } /// <summary> /// 生成静态页面 /// </summary> /// <param name="configFileName">配置文件路径</param> /// <param name="configTag">配置文件标记</param> /// <param name="templateFile">模板文件路径</param> /// <param name="param"></param> public bool MakeHtml(string configFileName, string configTag, string templateFile, string[] param) { var saveFilePath = SavePath + "/" + fileName; if (File.Exists(saveFilePath)) { DateTime dt = File.GetLastWriteTime(saveFilePath); TimeSpan ts = dt - DateTime.Now; if (ts.TotalHours < 1) return true; else DeleteHtml(saveFilePath); } int count = GetConfig(configFileName, configTag); string[] _replaceStr = new string[count]; try { FileStream tFile = File.Open(templateFile, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(tFile, Encoding.GetEncoding("utf-8")); StringBuilder sb = new StringBuilder(reader.ReadToEnd()); reader.Close(); for (int i = 0; i < count; i++) { sb.Replace("$replace[" + i + "]$", param[i]); } FileStream rFile = File.Create(SavePath + "/" + FileName); StreamWriter writer = new StreamWriter(rFile, Encoding.GetEncoding("utf-8")); writer.Write(sb.ToString()); writer.Flush(); writer.Close(); return true; } catch (Exception ex) { throw ex; return false; } } /// <summary> /// 删除静态文件 /// </summary> /// <param name="dirName"></param> public void DeleteHtml(string dirName) { File.Delete(dirName); } } public class CreateFilterHtml : ConvertToHtml { /// <summary> /// 构造函数 /// </summary> /// <param name="fileName"></param> /// <param name="saveUrl"></param> public CreateFilterHtml(string fileName, string saveUrl) { base.FileName = fileName + ".html"; base.SavePath = saveUrl; } /// <summary> /// 重载方法 /// </summary> /// <param name="param"></param> /// <returns></returns> public override bool CreateHtml(object param) { string[] paramvalue = param as string[]; Console.WriteLine("dfsfkdjfk"); var path = @"E:\动态页面静态化\ConsoleApplication1(模板的形式)\ConsoleApplication1"; var confilPath = path + "/config.xml"; var tempPath = path + "/temp.html"; return MakeHtml(confilPath, "article", tempPath, paramvalue); } } }
当使用的时候,需要建立相应的配置文件和模板文件,首先我们创建一个配置文件config.xml
<?xml version="1.0" encoding="utf-8" ?> <htmlWeb version="1"> <config> <article key="0" value="title"/> <article key="1" value="author"/> <article key="2" value="context"/> <article key="3" value="date"/> </config> </htmlWeb>
然后创建模板文件
<head> <title>模板文件</title> </head> <body> <h1>这是一个简单的HTML页,朋友们可以根据自己的需要重新设计</h1> <li>标题:$replace[0]$</li> <li>作者:$replace[1]$</li> <li>内容:$replace[2]$</li> <li>时间:$replace[3]$</li> </body>
使用方法为
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] param = new string[4]; param[0] = "测试模板"; param[1] = "农佳捷"; param[2] = "这是一个测试文章"; param[3] = "2007-10-30"; ConvertToHtml createHtml = new CreateFilterHtml("text", @"D:\test"); //单线程 createHtml.MakeHtmlFilter(param); Console.ReadKey(); } } }
此时就可以生成静态的html文件