asp.net 生成静态页面

目前asp.net常见webform中生成静态页面为伪静态或用后台代码生成。
1、伪静态也就是利用MS的URLRewriter.dll,进行URL的重写(这个我不常用,因为配置很多,很死板)
2、通过后台code实现
其实针对后台代码生成静态页面,其实很简单,原理可能都想的到,就是文件的创建、再创建,替换、再替换,也就是说我们将我们生成后的aspx文件替换成我们想要的.html或htm,但这样其实对于开发前端很难控制,也很麻烦。因此在我生成静态页面的时候,首先是做模板,做要生成静态页面的模板,用特殊符号代替需要动态变化的标签:
如:我们一个网站标头,您可以用以下代码作为替换标识

   <title>{$htmlTitle}</title>
    <meta name="keywords" content="{$htmlKeyWord}" />
    <meta name="describle" content="{$htmlDescrible}" />

以上代码中的带有{$$}的是进行后台替换的标识

后台定义模板code:

  //定义模板
            Array arryTemplete = Array.CreateInstance(typeof(System.String), 5);
            arryTemplete.SetValue("templete1", 0);
            arryTemplete.SetValue("templete2", 1);
            //定位自定义模板
            Array arryTempleteList = Array.CreateInstance(typeof(System.String), 5);
            arryTempleteList.SetValue("$index.html", 0);
            arryTempleteList.SetValue("$content.html", 1);
            arryTempleteList.SetValue("$list.html", 2);
            arryTempleteList.SetValue("$detail.html", 3);
            arryTempleteList.SetValue("$product.html", 4);

模板关键字替换code:


        private Dictionary<string, string> CreateHtml(HttpContext content, string templeteUrl, int indexShow, Menue menueMode)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string strHtml = CreatePageHtml(content, templeteUrl);
            string strMenue = string.Empty;
            //公共HTML
            DataTable dt = new ConfigBLL().GetList("1");
            dic.Add("{$htmlTitle$}", dt.Rows[0]["F_WebSiteName"].ToString());
            dic.Add("{$htmlKeyWord$}", dt.Rows[0]["F_KeyWord"].ToString());
            dic.Add("{$htmlDescrible$}", dt.Rows[0]["F_Describle"].ToString());
            dic.Add("{$htmlLogo$}", dt.Rows[0]["F_Logo"].ToString());
            dic.Add("{$htmlbanner$}", dt.Rows[0]["F_Banner"].ToString());
            dic.Add("{$htmlbannerdescrible$}", dt.Rows[0]["F_BannerDescrible"].ToString());
            dic.Add("{$htmlfooter$}", dt.Rows[0]["F_Footer"].ToString());
            //菜单HTML
            Menue mod = new Menue();
            dt = new MenueBLL().GetMenue(mod);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dic.Add("{$htmlmenue_" + (i + 1) + "$}", dt.Rows[i]["f_name"].ToString());
                dic.Add("{$htmlmenueurl_" + (i + 1) + "$}", dt.Rows[i]["f_url"].ToString());
            }
            //服务数据
            Modal.Service modService = new Modal.Service();
            modService.f_index = indexShow;
            dt = new ServiceBLL().GetList(modService);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dic.Add("{$htmlServiceName_" + (i + 1) + "$}", dt.Rows[i]["F_NAME"].ToString());
                if (dt.Rows[i]["F_DESCRIBLE"].ToString().Length > 80)
                {
                    dic.Add("{$htmlServiceDescrible_" + (i + 1) + "$}", dt.Rows[i]["F_DESCRIBLE"].ToString().Substring(0, 80) + "...");
                }
                else
                {
                    dic.Add("{$htmlServiceDescrible_" + (i + 1) + "$}", dt.Rows[i]["F_DESCRIBLE"].ToString());
                }
            }
            //图片数据
            Modal.Image modImg = new Modal.Image();
            modImg.f_index = indexShow;
            dt = new ImageBLL().GetList(modImg);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dic.Add("{$htmlimg_" + (i + 1) + "$}", dt.Rows[i]["F_URL"].ToString());
                dic.Add("{$htmlimgname_" + (i + 1) + "$}", dt.Rows[i]["F_NAME"].ToString());
            }
            //菜单对应的标题
            dic.Add("{$htmlContentTitle$}", menueMode.f_name);

            dic.Add("{$htmlContent$}", MakeContent(menueMode.fid));
            return dic;
        }

通过模板生成静态页面(文件创建操作):

 /// <summary>
        /// 生成HTML文件
        /// </summary>
        /// <param name="context"></param>
        /// <param name="templeteUrl">模板文件的的路径</param>
        /// <param name="dic">替换文件</param>
        /// <param name="fileName">生成的文件名</param>
        /// <returns></returns>
        public string CreatePageHtml(HttpContext context, string templeteUrl, Dictionary<string, string> dic, string fileName)
        {
            //----将html模版读入StringBuilder对象里面
            string result = string.Empty; ;
            StringBuilder htmltext = new StringBuilder();
            try
            {
                using (StreamReader sr = new StreamReader(context.Server.MapPath("../../Templete/" + templete + "/" + templeteUrl)))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        htmltext.Append(line);
                    }
                    sr.Close();
                }
            }
            catch
            {

            }
            //----------替换模板文件--------------------
            foreach (KeyValuePair<string, string> pair in dic)
            {
                htmltext.Replace(pair.Key, pair.Value);
            }
            //----------生成html文件------------------―― 
            try
            {
                using (StreamWriter sw = new StreamWriter(context.Server.MapPath(fileName), false, System.Text.Encoding.GetEncoding("UTF-8")))
                {
                    result = "1";
                    sw.WriteLine(htmltext);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch
            {

            }
            return result;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
…… 2.asp.net代码:   //---------------------读html模板页面到stringbuilder对象里----   string[] format=new string[4];//定义和htmlyem标记数目一致的数组   StringBuilder htmltext=new StringBuilder();   try   {    using (StreamReader sr = new StreamReader("存放模板页面的路径和页面名"))    {   String line;   while ((line = sr.ReadLine()) != null)   {    htmltext.Append(line);   }   sr.Close();    }   }   catch   {    Response.Write("<Script>alert('读取文件错误')</Script>");   }   //---------------------给标记数组赋值------------   format[0]="background="bg.jpg"";//背景图片   format[1]= "#990099";//字体颜色   format[2]="150px";//字体大小   format[3]= "<marquee>生成的模板html页面</marquee>";//文字说明   //----------替换htm里的标记为你想加的内容   for(int i=0;i<4;i )   {    htmltext.Replace("$htmlformat[" i "]",format[i]);   }   //----------生成htm文件------------------――   try   {    using(StreamWriter sw=new StreamWriter("存放路径和页面名",false,System.Text.Encoding.GetEncoding("GB2312")))   {    sw.WriteLine(htmltext);    sw.Flush();    sw.Close();   }   }   catch   {   Response.Write ("The file could not be wirte:");   }   小结   用此方法可以方便的生成html文件。程序使用了是循环替换,因此对需替换大量元素的模板速度非常快。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值