关于静态化网页

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Text; using System.IO; namespace KangMore.Web {     public static class HtmlStaticTools     {         /// <summary>         /// 输出静态页面         /// </summary>         /// <param name="htmlStaticEnum">静态化类型</param>         /// <param name="listConvertContent">标签以及转化内容列表</param>         /// <returns>返回新生成的静态页面URI</returns>         public static string OutputHtml(HtmlStaticEnum htmlStaticEnum, List<ConvertContent> listConvertContent, String htmlName)         {             //绝对路径             string filePath = "";             //相对Web容器路径             string fileUri = "";             //初始化名称             htmlName += ".html";             string staticTemplatePath = "";             string webPath = HttpContext.Current.Server.MapPath(@"..\");             string filePathKey = "HtmlStatic_" + htmlStaticEnum.ToString();             string staticTemplateKey = "StaticTemplate_" + htmlStaticEnum.ToString();                         //暂时不含日期             fileUri = String.Format(@"{0}"                 , ConfigurationManager.AppSettings[filePathKey].ToString()                 //, DateTime.Now.ToShortDateString()                               );             filePath = String.Format(@"{0}{1}"                               , webPath                               , fileUri                               );             staticTemplatePath = String.Format(@"{0}{1}"                 , webPath                 , ConfigurationManager.AppSettings[staticTemplateKey].ToString());             StringBuilder sbTemplateContent = new StringBuilder();             StreamReader sr = new StreamReader(staticTemplatePath);//建立文件读取流             String strTempLine;             while ((strTempLine = sr.ReadLine()) != null)             {                 sbTemplateContent.Append(strTempLine);             }             sr.Close();//关闭文件读取流             foreach (var item in listConvertContent)             {                 sbTemplateContent.Replace(item.Tag, item.Content);             }             if (!Directory.Exists(filePath))             {                 Directory.CreateDirectory(filePath);             }             fileUri = fileUri + @"\" + htmlName;             filePath = filePath + @"\" + htmlName;             StreamWriter sw = new StreamWriter(filePath, false, Encoding.Unicode);             sw.Write(sbTemplateContent.ToString());             sw.Close();             return @"..\" + fileUri;         }         /// <summary>         /// 查找是否已经存在静态化文件         /// </summary>         /// <param name="filePath">文件绝对路径</param>         /// <param name="htmlName">文件名</param>         /// <returns>是否存在</returns>         private static bool GetHtmlUri(string filePath, String htmlName)         {             if (!File.Exists(filePath+@"\"+htmlName))             {                 return false;             }             return true;         }         /// <summary>         /// 校验HTML文件是否存在         /// </summary>         /// <param name="htmlStaticEnum">静态化类型</param>         /// <param name="htmlName">文件名称</param>         /// <returns>若没找到则返回String.Empty 找到则返回相对Web容器的路径</returns>         public static string CheckHadHtml(HtmlStaticEnum htmlStaticEnum, string htmlName)         {             //绝对路径             string filePath = "";             //相对Web容器路径             string fileUri = "";             //初始化名称             htmlName += ".html";             string webPath = HttpContext.Current.Server.MapPath(@"..\");             string filePathKey = "HtmlStatic_" + htmlStaticEnum.ToString();             string staticTemplateKey = "StaticTemplate_" + htmlStaticEnum.ToString();             //暂时不含日期             fileUri = String.Format(@"{0}"                 , ConfigurationManager.AppSettings[filePathKey].ToString()                 //, DateTime.Now.ToShortDateString()                               );             filePath = String.Format(@"{0}{1}"                               , webPath                               , fileUri                               );                          if (GetHtmlUri(filePath, htmlName))             {                 return @"..\"+fileUri + @"\" + htmlName;             }             return "";         }     } }

 

静态页模版

 

<!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> <meta name="robots" content="all" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>静态页模版</title> </head> <body > <span>{name}</span> <span>{title}</span> </body></html>方法调用:

string uri = HtmlStaticTools.CheckHadHtml(HtmlStaticEnum.Disease, diseaseExtendID.ToString()); if (string.IsNullOrEmpty(uri)) { List<ConvertContent> list = new List<ConvertContent>(); list.Add(new ConvertContent("{name}", "xxxx")); list.Add(new ConvertContent("{title}", "页面静态化")); Response.Redirect(HtmlStaticTools.OutputHtml(HtmlStaticEnum.Disease, list, diseaseExtendID.ToString())); }else { Response.Redirect(uri); }

 

 
    

 

 
    

XML

 

 

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

 

 

枚举..

 

/// <summary> /// 静态化网页枚举 /// </summary> public enum HtmlStaticEnum { News, Hospital, Disease }

 

 
   
posted on 2011-11-02 15:07  刘剑_1989 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/newer/archive/2011/11/02/2324324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值