现在的位置: 首页 > 综合 > 正文

关于静态化网页

2017年10月11日 ⁄ 综合 ⁄ 共 4521字 ⁄ 字号 评论关闭
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
    }


抱歉!评论已关闭.