Asp.net生成htm静态文件的两种途径

现在很多网站特别是资讯类的都把内容生成静态页(htm\html\shtml等),这类总结了一下两种生成静态页的方法并做了一个Demo文件供大家下载。

分别是通过模板(比较常用)和根据url生成(不到万部则以不用,因为这中方式只能获取html的部分):

Asp.net生成静态文件(根据时间自动命名保持,默认扩展名是htm可以自行修改)。

通过收入内容替换模板或者url地址两种方式进行静态文件的生成

templete.htm为模板文件,htm为生成后的静态文件保存位置

这类粘贴出.cs文件

  1 None.gif   // 51aspx.com生成静态页演示文件,转载请保留该信息
  2 None.gif      public  partial  class  _Default : System.Web.UI.Page
  3 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
  4InBlock.gif        protected void Page_Load(object sender, EventArgs e)
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  6InBlock.gif           
  7ExpandedSubBlockEnd.gif        }

  8InBlock.gif
  9InBlock.gif        //根据模板生成,保持在html文件夹中(部分源码搜集于网络)
 10InBlock.gif        protected void Button1_Click(object sender, EventArgs e)
 11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 12InBlock.gif            //源码是替换掉模板中的特征字符
 13InBlock.gif
 14InBlock.gif            string mbPath =Server.MapPath("template.htm");
 15InBlock.gif            Encoding code = Encoding.GetEncoding("gb2312");
 16InBlock.gif            StreamReader sr = null;
 17InBlock.gif            StreamWriter sw = null;
 18InBlock.gif            string str = null;
 19InBlock.gif
 20InBlock.gif            //读取
 21InBlock.gif            try
 22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 23InBlock.gif                sr = new StreamReader(mbPath, code);
 24InBlock.gif                str = sr.ReadToEnd();
 25InBlock.gif
 26ExpandedSubBlockEnd.gif            }

 27InBlock.gif            catch (Exception ex)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 29InBlock.gif                throw ex;
 30ExpandedSubBlockEnd.gif            }

 31InBlock.gif            finally
 32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 33InBlock.gif                sr.Close();
 34ExpandedSubBlockEnd.gif            }

 35InBlock.gif
 36InBlock.gif            //根据时间自动重命名,扩展名也可以自行修改
 37InBlock.gif            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss"+ ".htm";
 38InBlock.gif            str = str.Replace("$title$", txtTitle.Text);//替换Title
 39InBlock.gif            str = str.Replace("$content$", txtContent.Text);//替换content
 40InBlock.gif
 41InBlock.gif            //生成静态文件
 42InBlock.gif            try
 43ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 44InBlock.gif                sw = new StreamWriter(Server.MapPath("htm/"+ fileName, false, code);
 45InBlock.gif                sw.Write(str);
 46InBlock.gif                sw.Flush();
 47InBlock.gif
 48ExpandedSubBlockEnd.gif            }

 49InBlock.gif            catch (Exception ex)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 51InBlock.gif                throw ex;
 52ExpandedSubBlockEnd.gif            }

 53InBlock.gif            finally
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                sw.Close();
 56InBlock.gif                Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已经生成,保存在htm文件夹下!");
 57ExpandedSubBlockEnd.gif            }

 58InBlock.gif
 59
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif
 62InBlock.gif
 63InBlock.gif        //根据Url地址生成静态页保持
 64InBlock.gif        protected void Button2_Click(object sender, EventArgs e)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            Encoding code = Encoding.GetEncoding("utf-8");
 67InBlock.gif            StreamReader sr = null;
 68InBlock.gif            StreamWriter sw = null;
 69InBlock.gif            string str = null;
 70InBlock.gif
 71InBlock.gif            //读取远程路径
 72InBlock.gif            WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
 73InBlock.gif            WebResponse myTemp = temp.GetResponse();
 74InBlock.gif            sr = new StreamReader(myTemp.GetResponseStream(), code);
 75InBlock.gif            //读取
 76InBlock.gif            try
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                sr = new StreamReader(myTemp.GetResponseStream(), code);
 79InBlock.gif                str = sr.ReadToEnd();
 80InBlock.gif
 81ExpandedSubBlockEnd.gif            }

 82InBlock.gif            catch (Exception ex)
 83ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 84InBlock.gif                throw ex;
 85ExpandedSubBlockEnd.gif            }

 86InBlock.gif            finally
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 88InBlock.gif                sr.Close();
 89ExpandedSubBlockEnd.gif            }

 90InBlock.gif            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss"+ ".htm";
 91InBlock.gif
 92InBlock.gif            //写入
 93InBlock.gif            try
 94ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 95InBlock.gif                sw = new StreamWriter(Server.MapPath("htm/"+ fileName, false, code);
 96InBlock.gif                sw.Write(str);
 97InBlock.gif                sw.Flush();
 98InBlock.gif
 99ExpandedSubBlockEnd.gif            }

100InBlock.gif            catch (Exception ex)
101ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
102InBlock.gif                throw ex;
103ExpandedSubBlockEnd.gif            }

104InBlock.gif            finally
105ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
106InBlock.gif                sw.Close();
107InBlock.gif                Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
108ExpandedSubBlockEnd.gif            }

109InBlock.gif
110ExpandedSubBlockEnd.gif        }

111ExpandedBlockEnd.gif    }

只是一个Demo文件,仅供大家参考,也希望有其他生成方式的也讨论一下(部分源码搜集于网络)

完整项目下载地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值