http://www.cnblogs.com/tonycall/archive/2009/07/18/1526079.html

第一次发表,有什么错误,请大家谅解噢!

如果不明白的话,建议自己拷一次。 就会的了。。

开发步骤:

1、路径映射类(UrlMapping),主要对路径进行拆分、拼接。(关键的一步)

2、过滤流类(FilterStream),主要负责生成静态页面。

3、静态页面类(HtmlPage),主要是调用UrlMapping和FilterStream类,

哪个页面想静态化,就继承这个类。

4、HtmlHandler类,路径后缀为Html的,都由它来处理,与HtmlPage类相似。

5、HtmlPanel类(控件),页面带上这个控件,超链接会静态化。(详情请下载源码包)

部分代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO; namespace Eshop.Web.UI
{
    /// <summary>
    /// 路径映射
    /// </summary>
    public static class UrlMapping
    {
        //Aspx 转换到 Html
        public static string AspxToHtml(string url)
        {
            //判断路径是否为空
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("路径不能为空");
            }             //分割路径
            string[] temp = url.Split('?');             if (temp.Length !=  && temp.Length != )
            {
                throw new ArgumentException(String.Format("路径 {0} 及其参数错误", url));
            }             //获取路径后缀
            string ext = Path.GetExtension(temp[]);    
            if (!(ext.Equals(".aspx", StringComparison.OrdinalIgnoreCase)))
            {
                throw new ArgumentException(String.Format("路径 {0} 类型必须为ASPX", url));
            }             //截取.aspx中前面的内容
            int offset = temp[].LastIndexOf('.');
            string resource = temp[].Substring(, offset);             //路径不带参数时
            if (temp.Length ==  || string.IsNullOrEmpty(temp[]))
            {
                return string.Format("{0}.html", resource);    //拼接
            }             //路径带参数时
            return string.Format("{0}___{1}.html", resource, temp[]); //拼接
        }
        
        //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 == )
            {
                return string.Format("{0}.aspx", temp[]);
            }             if (temp.Length == )
            {
                return String.Format("{0}.aspx?{1}", temp[], temp[]);
            }             throw new ArgumentException(String.Format("资源 {0} 及其参数错误", url));
        }
    }
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO; namespace Eshop.Web.UI
{
    /// <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();
            }
        }
    }
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO; namespace Eshop.Web.UI
{
    /// <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);
        }
    }
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO; namespace Eshop.Web.UI  
{
    /// <summary>
    /// 后缀为HTML的,都经这里处理
    /// web.config
    /// <remove verb="*" path="*.HTML"/>
    /// <add verb="*" path="*.HTML" type="Eshop.Web.UI.HtmlHandler,AspxToHtmlDemo"/>
    /// </summary>
    public class HtmlHandler:IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }         /// <summary>
        /// 获取物理路径,判断文件夹中有没有存在这个文件
        /// 不存在的话,就会调用FilterStream类进行创建,并写入内容
        /// 存在的话,就直接显示页面
        /// </summary>
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;             string htmlPage = request.RawUrl;
            string htmlFile = context.Server.MapPath(htmlPage);             if (File.Exists(htmlFile))
            {
                response.WriteFile(htmlFile);
                return;
            }             //Html 文件不存在
            string aspxPage = UrlMapping.HtmlToAspx(htmlPage);
            response.Redirect(aspxPage);
        }     }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Eshop.Web.Index" %>

<!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 runat="server">
    <title>AspxToHtml Demo</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>AspxToHtml Demo</h1>
        <br />
        
        <html:HtmlPanel ID="hp" runat="server">
            <asp:HyperLink ID="Hy" runat="server" NavigateUrl="~/Index.aspx?page=2">
                   点击
            </asp:HyperLink>
            <br />
            <a href="~/Index.aspx?page=2" runat="server">Hello</a>
        </html:HtmlPanel>
    </div>
    </form>
</body>
</html>

源码包:   /Files/tonycall/AspxToHtmlDemo.zip

Asp.net 生成静态页面的更多相关文章

  1. 三种C#.net生成静态页面的方法

    ASP.NET生成静态页面方法主要有三种   第一种方法:向服务器的动态页面发送请求,获取页面的html代码.这种方法缺点显而易见:速度慢.另外如果请求的动态页面有验证控件的话,返回的html页面却无 ...

  2. asp.net配置全局应用程序类 巧妙达到定时生成静态页面

    //在项目里添加一个"全局应用程序类(Global Application Class)",在里面写这样的代码: public class Global : System.Web. ...

  3. .NET生成静态页面并分页

    因为公司的产品用asp开发, 前一段时间用asp写了一个生成静态页面并分页的程序,但缘于对.net的热爱,写了这个.net下的生成静态页面并分页的程序. 主要的原理就是替换模板里的特殊字符. 1.静态 ...

  4. .net 生成 静态页面

    .net 生成 静态页面 <!--Main.Aspx--> <%@ page language="C#" %> <%@ import namespac ...

  5. .NET生成静态页面例子

    主要做法如下: 1.创建网站,并创建一个模板页,template.htm 2.添加一个web窗体Default.aspx 3.在网站下新建文件夹htm,设置该文件夹的属性,确保该文件夹具有可写权限 详 ...

  6. 浅谈php生成静态页面

    一.引 言 在速度上,静态页面要比动态页面的比方php快很多,这是毫无疑问的,但是由于静态页面的灵活性较差,如果不借助数据库或其他的设备保存相关信息的话,整体的管理上比较繁琐,比方修改编辑.比方阅读权 ...

  7. C#根据网址生成静态页面

    HoverTree开源项目中HoverTreeWeb.HVTPanel的Index.aspx文件 是后台管理的首页. 包含生成留言板首页,以及显示用户名,退出等功能. 根据网址生成页面的方法: boo ...

  8. 用 Smarty 生成静态页面入门介绍

    why Smarty? 随着公司首页(以下简称首页)流量越来越大,最近开始考虑使用后台语言生成静态页面的技术. 我们知道,一个简单页面一般是一个 .html(或者 .htm ..shtml)后缀的文件 ...

  9. 比较详细PHP生成静态页面教程

    一,PHP脚本与动态页面. PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与HTML文件混合, 也可以类,函数封装等形式,以模板的方式对用户请求进行处理.无论以何种方式,它的基本原理是这样的.由客 ...

随机推荐

  1. 关于django Class-based views的理解

    django是mvt模式,其中v就是这个显示逻辑部分,简单来讲,view函数可以说是接收request,然后处理,返回response的主体函数. 对于一些简单的逻辑关系,可以用直接用函数模式来进行处 ...

  2. XMOJ 1133: 膜拜大牛 计算几何/两圆相交

    1133: 膜拜大牛 Time Limit: 1 Sec  Memory Limit: 131072KiBSubmit: 9619  Solved: 3287 题目连接 http://acm.xmu. ...

  3. 使用TensorFlow高级别的API进行编程

    这里涉及到的高级别API主要是使用Estimator类来编写机器学习的程序,此外你还需要用到一些数据导入的知识. 为什么使用Estimator Estimator类是定义在tf.estimator.E ...

  4. 最小生成树-普利姆算法lazy实现

    算法描述 lazy普利姆算法的步骤: 1.从源点s出发,遍历它的邻接表s.Adj,将所有邻接的边(crossing edges)加入优先队列Q: 2.从Q出队最轻边,将此边加入MST. 3.考察此边的 ...

  5. 虚拟信用卡 全球付, 工商银行国际E卡, Bancore, Entropay, Payoneer

    虚拟信用卡 海外网购.购买国外域名空间.ebay等一些国外网站账号的激活这些情况都需要用到国际信用卡, 如果没有信用卡或者有信用卡但是对于安全性有所顾虑怎么办? 其实有一种东西叫做虚拟信用卡,正规银行 ...

  6. 不使用nib 文件时,需要改变一个view 的大小时,需要为viewcontroller添加loadView方法

    - (void)loadView{ self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.v ...

  7. Selenium2+python自动化47-判断弹出框存在(alert_is_present)

    前言 系统弹窗这个是很常见的场景,有时候它不弹出来去操作的话,会抛异常.那么又不知道它啥时候会出来,那么久需要去判断弹窗是否弹出了. 本篇接着Selenium2+python自动化42-判断元素(ex ...

  8. 关于OpenLDAPAdmin管理页面提示“This base cannot be created with PLA“问题. Strong Authentication Required问题

    经过查询,最终总结和处理如下: 1.首先需要在/etc/openldap/目录下,创建一个base.ldif文件,如下所示: 2.在base.ldif文件中,写入如下信息,为创建初始化根节点做准备工作 ...

  9. eclipse中文字体大小修改,让中英文字体协调

    貌似有不少人苦恼eclipse中文字体大小修改问题,默认的eclipse中文字体很小,和英文字体大小完全不在一个调子上,因为默认的eclipse juno中英文字体是Consolas,字体大小是10, ...

  10. 阿里云云盾抗下全球最大DDoS攻击(5亿次请求,95万QPS HTTPS CC攻击) ,阿里百万级QPS资源调度系统,一般的服务器qps多少? QPS/TPS/并发量/系统吞吐量

    阿里云云盾抗下全球最大DDoS攻击(5亿次请求,95万QPS HTTPS CC攻击) 作者:用户 来源:互联网 时间:2016-03-30 13:32:40 安全流量事件https互联网资源 摘要:  ...