.NET生成静态页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace testDataSet
{
    public partial class ToHtml : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //可以根据id生成静态页面
            //int id;
            //  try
            //  {
            //    id = int.Parse (Request.QueryString["id"]);
            //  }
            //  catch
            //  {
            //    throw (new Exception ("页面没有指定id"));
            //  }
 
            //  string filename=Server.MapPath("StaticHtml_"+id+".html");
               string filename=Server.MapPath("StaticHtml.html");
              Stream s = GetFileStream (filename);//尝试读取已有文件
              if (s != null)//如果文件存在并且读取成功
              {
                using (s)
                {
                  Stream2Stream (s, Response.OutputStream);///从文件到本页面
                  Response.End ();
                }
              }
              //调用Main_Execute,并且获取其输出
              StringWriter sw = new StringWriter ();
             // Server.Execute("SQLtoXML.aspx?id="+id, sw);//执行 SQLtoXML.aspx 页面,并将内容存入 sw 中


              Server.Execute("SQLtoXML.aspx", sw);//执行 SQLtoXML.aspx 页面,并将内容存入 sw 中
              string content = sw.ToString ();
              Response.Write(content);//输出到客户端
              Response.Flush();
 
              //写进文件
 
              try
              {
                using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write))
                {
                  using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding))
                  {
                    streamwriter.Write (content);
                  }
                }
              }
              finally
              {
                //Response.End ();
              }
            }

        /// <summary>
        /// 输出文件内容
        /// </summary>
        /// <param name="src">文件源</param>
        /// <param name="dst">输出目的地</param>
            static public void Stream2Stream (Stream src, Stream dst)
            {
              byte[] buf = new byte[4096];
              while (true)
              {
                int c = src.Read (buf, 0, buf.Length);
                if(c==0)
                  return;
                dst.Write (buf, 0, c);
              }
            }

        /// <summary>
        /// 得到5分钟之内的内容 ,超过5分钟返回null
        /// </summary>
        /// <param name="filename">要生成的目标文件</param>
        /// <returns>返回文件内容 或 null</returns>
            public Stream GetFileStream(string filename)
            {
              try
              {
                DateTime dt = File.GetLastWriteTime (filename);//得到文件的最后写入时间
                TimeSpan ts= DateTime.Now - dt;
                if(ts.TotalMinutes>5)
                  return null;    //5分钟后过期
                return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
              }
              catch
              {
                return null;
              }
            }
        }
}

 

效果:运行后生成的html页

.NET生成静态页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值