c#生成静态页面

生成静态页面,

 1.新建类HtmlPage

 /// <summary>
    /// 哪个页面想静态化,就继承这个类
    /// </summary>
    public class HtmlPage:Page
    {
        // <summary>
        /// 获取物理路径,判断文件夹中有没有存在这个文件
        /// 不存在的话,就会调用FilterStream类进行创建,并写入内容
        /// 存在的话,就直接显示页面
        /// </summary>
        public override void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            string htmlPage = UrlMapping.AspxToHtml(req.RawUrl);
            string htmlFile = context.Server.MapPath(htmlPage);
            if (File.Exists(htmlFile))
            {
                resp.Redirect(htmlPage);
                return;
            }
            // Html 页面不存在
            resp.Filter = new FilterStream(resp.Filter, htmlFile);
            base.ProcessRequest(context);
        }
    }

2 .新建类FilterStream

    /// <summary>
    /// 静态网页保存
    /// </summary>
    public class FilterStream : Stream
    {
        private Stream respStream = null;
        private Stream fileStream = null;
        public FilterStream(Stream respStream, string filePath)
        {
            if (respStream == null)
                throw new ArgumentNullException("输出流不能为空");
            this.respStream = respStream;  
            try
            {
                this.fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);  //写入到文件夹中
            }
           catch { }
        }
        public override bool CanRead
        {
            get { return this.respStream.CanRead; }
        }
        public override bool CanSeek
        {
            get { return this.respStream.CanSeek; }
        }
        public override bool CanWrite
        {
            get { return this.respStream.CanWrite; }
        }
        public override void Flush()
        {
            this.respStream.Flush();
            if (this.fileStream != null)
            {
                this.fileStream.Flush();
            }
        }
        public override long Length
        {
            get { return this.respStream.Length; }
        }
        public override long Position
        {
            get
            {
                return this.respStream.Position;
            }
            set
            {
                this.respStream.Position = value;

                if (this.fileStream != null)
                {
                    this.fileStream.Position = value;
                }
            }
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            return this.respStream.Read(buffer, offset, count);
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            if (this.fileStream != null)
            {
                this.fileStream.Seek(offset, origin);
            }
            return this.respStream.Seek(offset, origin);
        }
        public override void SetLength(long value)
        {
            this.respStream.SetLength(value);
            if (this.fileStream != null)
            {
                this.fileStream.SetLength(value);
            }
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            this.respStream.Write(buffer, offset, count);
            if (this.fileStream != null)
            {
                this.fileStream.Write(buffer, offset, count);
            }
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            this.respStream.Dispose();
            if (this.fileStream != null)
            {
                this.fileStream.Dispose();
            }
        }
    }

3.新建类 UrlMapping

        //Aspx 转换到 Html
        public static string AspxToHtml(string url)
        {
           //判断路径是否为空
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("路径不能为空");
            }
           //分割路径
            string[] temp = url.Split('?');
            if (temp.Length != 1 && temp.Length != 2)
            {
                throw new ArgumentException(String.Format("路径 {0} 及其参数错误", url));
            }
            //获取路径后缀
            string ext = Path.GetExtension(temp[0]);    
            if (!(ext.Equals(".aspx", StringComparison.OrdinalIgnoreCase)))
            {
                throw new ArgumentException(String.Format("路径 {0} 类型必须为ASPX", url));
            }
            //截取.aspx中前面的内容
            int offset = temp[0].LastIndexOf('.');
            string resource = temp[0].Substring(0, offset);
            //路径不带参数时
            if (temp.Length == 1 || string.IsNullOrEmpty(temp[1]))
            {
                return string.Format("{0}.html", resource);    //拼接
              }
            //路径带参数时
            return string.Format("{0}___{1}.html", resource, temp[1]); //拼接
        }   
        //Html 转换到 Aspx
        public static string HtmlToAspx(string url)
        {
            //判断路径是否为空
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("路径不能为空");
            }
            string ext = Path.GetExtension(url);
            if (!(ext.Equals(".html", StringComparison.OrdinalIgnoreCase)))
            {
                throw new ArgumentException(String.Format("路径 {0} 类型必须为HTML", url));
            }
            string[] temp = url.Split(new String[] { "___", "." }, StringSplitOptions.RemoveEmptyEntries);
            if (temp.Length == 2)
            {
                return string.Format("{0}.aspx", temp[0]);
            }
            if (temp.Length == 3)
            {
                return String.Format("{0}.aspx?{1}", temp[0], temp[1]);
            }
            throw new ArgumentException(String.Format("资源 {0} 及其参数错误", url));
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值