将asp.net页面静态化有很大意义,在些我就不多说了,实现asp.net页面静态化有很多种方面,在此我介绍一种在asp.net内实现的方法。大家都知道,每个asp.net页面都有一个特定的类对其进行处理,默认情况下该类是从Page类派生的。我们所要做的就是从Page类派生自己的类改写特定的方法。具体实现如下:

using System;
using System.Data;
using System.Configuration;
using System.Linq;
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.Xml.Linq;
using System.IO;

public class FreezablePage : System.Web.UI.Page
{
    // When Asp.Net renders the page the Page.Render method is invoked
    // Override the method to hook in

protected void Page_Load(object sender, EventArgs e)
    {
        //Response.HeaderEncoding = System.Text.Encoding.UTF8;
        //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

Freeze(string.Format(@"{0}.htm", Request.Url.ToString()));

}

protected override void Render(HtmlTextWriter writer)
    {
        if (freeze)
        {
            MyHtmlFileCreator htmlFile = new MyHtmlFileCreator();
            // Let Asp.net render the output, catch it in the file creator
            base.Render(htmlFile.RenderHere);

newUrl = HttpContext.Current.Request.Url.AbsolutePath.ToString();
            newUrl = newUrl.Replace(".aspx", ".htm");

//Response.HeaderEncoding = System.Text.Encoding.UTF8;
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            // Write new html file
            htmlFile.WriteHTMLFile(Server.MapPath(newUrl));
            // Redirect

Response.Redirect(newUrl, true);
        }
        else
        {
            // Default behavior
            base.Render(writer);
        }

}

// Flag render event
    protected void Freeze()
    {
        freeze = true;
    }

protected void Freeze(string toUrl)
    {
        freeze = true;
        NewUrl = toUrl;
    }

private bool freeze = false;

private string newUrl;

internal string NewUrl
    {
        get
        {
            return newUrl;
        }
        set
        {
            newUrl = value;
        }

}

}
internal class MyHtmlFileCreator
{
    private StringWriter html;
    private MyHtmlTextWriter htmlWriter;

// override the HtmlTextWriter to reach the constructor
    // the constructor in the base class is protected
    class MyHtmlTextWriter : HtmlTextWriter
    {
        internal MyHtmlTextWriter(TextWriter tw) : base(tw) { }
    }
    // publish the HTMLwriter
    internal HtmlTextWriter RenderHere
    {
        get { return htmlWriter; }
    }
    // constructor initializes stringwriter and htmlwriter based on that
    // initialize Url
    internal MyHtmlFileCreator()
    {
        html = new StringWriter();
        htmlWriter = new MyHtmlTextWriter(html);
    }

internal void WriteHTMLFile(string virtualFileName)
    {
        // Stringreader reads output rendered by asp.net
        // Stringwriter writes html output file
        StringReader sr = new StringReader(html.ToString());
        StringWriter sw = new StringWriter();
        // Read from input
        string htmlLine = sr.ReadLine();
        while (htmlLine != null)
        {
            /**/ Filter out ASp.net specific tags
            //if (!((htmlLine.IndexOf("<form") > 0) ||
            //      (htmlLine.IndexOf("__VIEWSTATE") > 0) ||
            //      (htmlLine.IndexOf("</form>") > 0)))
            //{ sw.WriteLine(htmlLine); }
            sw.WriteLine(htmlLine);
            htmlLine = sr.ReadLine();
        }
        // Write contents stringwriter to html file
        StreamWriter fs = new StreamWriter(virtualFileName,false,System.Text.Encoding.UTF8);

//StreamWriter f=new StreamWriter(
        fs.Write(sw.ToString());
        fs.Close();
    }

}

你只要用此类做为你的页面的基类就可实现页面的静态化了,但此方法有一个缺点,就是他频繁的读写文件,会造成性能的下降。

转载于:https://www.cnblogs.com/ayxiaopan/articles/2018811.html

【转摘留用】页面静态化..相关推荐

  1. 项目性能优化(实现页面静态化1)

    当首页访问频繁,而且查询数据量大,其中还有大量的循环处理时,这会耗费服务器大量的资源,并且响应数据的效率,这时就需要页面静态化. 1. 页面静态化介绍 1.为什么要做页面静态化 减少数据库查询次数. ...

  2. 【工具类】页面静态化 --- Freemarker的使用

    介绍 FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP. ...

  3. [Apache]网站页面静态化与Apache调优(图)

    ---------------------------------------------------------------------------------------------------- ...

  4. 一步一步asp.net_页面静态化管理

    最近事情多,中间还生病了一次,纠结,最近一年来都没有什么毛病,不知道咋了...头痛..... 今天闲下来写篇日志,页面静态化. 页面静态化是我们经常碰到的问题,在web中,要说速度,只有html静态页 ...

  5. Freemarker商品页面静态化

    商品页面静态化 静态化资源,不需要Tomcat容器 通过nginx提供http服务,就可以访问 输出文件路径,不需要在工程中,定义一个外部路径 输出文件名称,商品id+.html 比如,京东的每一个商 ...

  6. 赋值后页面不渲染_第七节:框架搭建之页面静态化的剖析

    一. 前言 抛砖引玉: 提到项目性能优化,大部分人第一时间就会想到缓存,针对"读多写少"的数据,可以放到缓存里,设置个过期时间,这样就不用每次都去数据库中查询了, 减轻了数据库的压 ...

  7. 页面静态化2 --- 使用PHP缓存机制来完成页面静态化(上)(ob_flush和flush函数区别用法)...

    我们可以使用PHP自带的缓存机制来完成页面静态化,但在这里,需要说明一点,仅靠PHP缓存机制并不能完美的解决页面静态化,往往需要和其他页面静态技术(通常是伪静态技术)结合使用 例子: 当访问一个页面时 ...

  8. [导入]做了一个页面静态化小软件,和大家分享,up有分

    简单介绍:本人[color=#FF0000]中关村生活网站长[/color],[url=http://www.zgclive.com][/url]在做站的过程中,不可避免会遇到页面静态化的问题,以前也 ...

  9. build vue 静态化_页面静态化

    页面静态化和静态缓存 静态缓存:还需要请求php一些简单的判断,只是一些复杂的逻辑结构不需要再进行php处理.如在缓存数据库中的数据,这样就不用每次都请求数据库.典型的例子就是smarty中有页面静态 ...

最新文章

  1. 修复计算机怎么操作系统,如何巧妙恢复被误删的操作系统分区
  2. C/S架构应用程序开发培训笔记
  3. ORACLE 数据迁移
  4. Java学习小程序(9)冒泡排序算法实现
  5. mac下IPhone开发环境配置
  6. 免安装免配置 还免费的Spark 集群 --Databrickes Spark Clould
  7. 动态规划 0-1背包问题 二维数组
  8. notepad++节点_在C ++中删除链接列表的中间节点
  9. 驱动重构SDN/NFV奠定未来网络基石
  10. 关于mysql叙述中错误的是什么_以下关于MySQL的叙述中,错误的是( )。_学小易找答案...
  11. C++11 非成员函数begin()、end()
  12. nvme驱动架构分析1
  13. html 百分比 rem,rem的坑,为什么要设置成百分比,为什么又是62.5%
  14. Python实现抓取CSDN热门文章列表
  15. Requirement already satisfied解决办法
  16. c++(标准模板库STL)
  17. 苹果 Apple Beta 版软件计划 相关软件地址
  18. 名悦集团:跟车行驶如何避免出现汽车追尾事故
  19. C++是TIOBE 2022年度最佳编程语言!
  20. Linux 下防火墙的作用

热门文章

  1. Flex scroller皮肤的使用
  2. Linux 自动删除N天前的文件
  3. 小谈RTMP中AMF3类型的Command message
  4. android 自定义MP4播放器
  5. Python架构(一)
  6. mysql索引实现原理
  7. 暑假集训考试反思+其它乱写
  8. 20165203 《网络对抗技术》week1 Kali的安装与配置
  9. ajax图片上传(asp.net +jquery+ashx)
  10. 洛谷 P1886 滑动窗口