关于静态化网页

 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Configuration;  
  6. using System.Text;  
  7. using System.IO;  
  8.   
  9.   
  10.   
  11.   
  12. namespace KangMore.Web  
  13. {  
  14.     public static class HtmlStaticTools  
  15.     {  
  16.   
  17.   
  18.         /// <summary>   
  19.         /// 输出静态页面   
  20.         /// </summary>   
  21.         /// <param name="htmlStaticEnum">静态化类型</param>   
  22.         /// <param name="listConvertContent">标签以及转化内容列表</param>   
  23.         /// <returns>返回新生成的静态页面URI</returns>   
  24.         public static string OutputHtml(HtmlStaticEnum htmlStaticEnum, List<ConvertContent> listConvertContent, String htmlName)  
  25.         {  
  26.             //绝对路径   
  27.             string filePath = "";  
  28.             //相对Web容器路径   
  29.             string fileUri = "";  
  30.             //初始化名称   
  31.             htmlName += ".html";  
  32.             string staticTemplatePath = "";  
  33.             string webPath = HttpContext.Current.Server.MapPath(@"..\");  
  34.   
  35.   
  36.             string filePathKey = "HtmlStatic_" + htmlStaticEnum.ToString();  
  37.             string staticTemplateKey = "StaticTemplate_" + htmlStaticEnum.ToString();  
  38.              
  39.   
  40.   
  41.             //暂时不含日期   
  42.             fileUri = String.Format(@"{0}"  
  43.                 , ConfigurationManager.AppSettings[filePathKey].ToString()  
  44.                 //, DateTime.Now.ToShortDateString()   
  45.                               );  
  46.   
  47.   
  48.             filePath = String.Format(@"{0}{1}"  
  49.                               , webPath  
  50.                               , fileUri  
  51.                               );  
  52.   
  53.   
  54.             staticTemplatePath = String.Format(@"{0}{1}"  
  55.                 , webPath  
  56.                 , ConfigurationManager.AppSettings[staticTemplateKey].ToString());  
  57.   
  58.   
  59.   
  60.   
  61.             StringBuilder sbTemplateContent = new StringBuilder();  
  62.             StreamReader sr = new StreamReader(staticTemplatePath);//建立文件读取流   
  63.             String strTempLine;  
  64.             while ((strTempLine = sr.ReadLine()) != null)  
  65.             {  
  66.                 sbTemplateContent.Append(strTempLine);  
  67.             }  
  68.             sr.Close();//关闭文件读取流   
  69.   
  70.   
  71.             foreach (var item in listConvertContent)  
  72.             {  
  73.                 sbTemplateContent.Replace(item.Tag, item.Content);  
  74.             }  
  75.   
  76.   
  77.             if (!Directory.Exists(filePath))  
  78.             {  
  79.                 Directory.CreateDirectory(filePath);  
  80.             }  
  81.   
  82.   
  83.             fileUri = fileUri + @"\" + htmlName;  
  84.             filePath = filePath + @"\" + htmlName;  
  85.             StreamWriter sw = new StreamWriter(filePath, false, Encoding.Unicode);  
  86.             sw.Write(sbTemplateContent.ToString());  
  87.             sw.Close();  
  88.   
  89.   
  90.             return @"..\" + fileUri;  
  91.   
  92.   
  93.         }  
  94.   
  95.   
  96.         /// <summary>   
  97.         /// 查找是否已经存在静态化文件   
  98.         /// </summary>   
  99.         /// <param name="filePath">文件绝对路径</param>   
  100.         /// <param name="htmlName">文件名</param>   
  101.         /// <returns>是否存在</returns>   
  102.         private static bool GetHtmlUri(string filePath, String htmlName)  
  103.         {  
  104.             if (!File.Exists(filePath+@"\"+htmlName))  
  105.             {  
  106.                 return false;  
  107.             }  
  108.             return true;  
  109.         }  
  110.   
  111.   
  112.         /// <summary>   
  113.         /// 校验HTML文件是否存在   
  114.         /// </summary>   
  115.         /// <param name="htmlStaticEnum">静态化类型</param>   
  116.         /// <param name="htmlName">文件名称</param>   
  117.         /// <returns>若没找到则返回String.Empty 找到则返回相对Web容器的路径</returns>   
  118.         public static string CheckHadHtml(HtmlStaticEnum htmlStaticEnum, string htmlName)  
  119.         {  
  120.             //绝对路径   
  121.             string filePath = "";  
  122.             //相对Web容器路径   
  123.             string fileUri = "";  
  124.             //初始化名称   
  125.             htmlName += ".html";  
  126.             string webPath = HttpContext.Current.Server.MapPath(@"..\");  
  127.   
  128.   
  129.             string filePathKey = "HtmlStatic_" + htmlStaticEnum.ToString();  
  130.             string staticTemplateKey = "StaticTemplate_" + htmlStaticEnum.ToString();  
  131.   
  132.   
  133.             //暂时不含日期   
  134.             fileUri = String.Format(@"{0}"  
  135.                 , ConfigurationManager.AppSettings[filePathKey].ToString()  
  136.                 //, DateTime.Now.ToShortDateString()   
  137.                               );  
  138.   
  139.   
  140.             filePath = String.Format(@"{0}{1}"  
  141.                               , webPath  
  142.                               , fileUri  
  143.                               );  
  144.               
  145.             if (GetHtmlUri(filePath, htmlName))  
  146.             {  
  147.                 return @"..\"+fileUri + @"\" + htmlName;  
  148.             }  
  149.             return "";  
  150.         }  
  151.   
  152.   
  153.   
  154.   
  155.     }  
  156. }  


静态页模版

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <meta name="robots" content="all" />  
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6.     <title>静态页模版</title>  
  7.   
  8. </head>  
  9. <body >  
  10.         <span>{name}</span>  
  11.         <span>{title}</span>  
  12. </body></html>  
方法调用:

  1. string uri = HtmlStaticTools.CheckHadHtml(HtmlStaticEnum.Disease, diseaseExtendID.ToString());  
  2.                     if (string.IsNullOrEmpty(uri))  
  3.                     {  
  4.                         List<ConvertContent> list = new List<ConvertContent>();  
  5.                         list.Add(new ConvertContent("{name}", "xxxx"));  
  6.                         list.Add(new ConvertContent("{title}", "页面静态化"));  
  7.             Response.Redirect(HtmlStaticTools.OutputHtml(HtmlStaticEnum.Disease, list, diseaseExtendID.ToString()));  
  8.             }else  
  9.             {  
  10.             Response.Redirect(uri);  
  11.   
  12.             }  


  1. <pre>  

 XML 

  1. <!--静态化地址-->  
  2. <add key="HtmlStatic_News" value="NewsHtmls"/>  
  3. <add key="HtmlStatic_Hospital" value="HospitalHtmls"/>  
  4. <add key="HtmlStatic_Disease" value="DiseaseHtmls"/>  
  5. <!--静态化模板-->  
  6. <add key="StaticTemplate_News" value="Template\News.htm"/>  
  7. <add key="StaticTemplate_Hospital" value="Template\Hospital.htm"/>  
  8. <add key="StaticTemplate_Disease" value="Template\Disease.htm"/>  


枚举..

  1. /// <summary>   
  2. /// 静态化网页枚举   
  3. /// </summary>   
  4. public enum HtmlStaticEnum  
  5. {  
  6.     News,  
  7.     Hospital,  
  8.     Disease  
  9. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值