zoukankan      html  css  js  c++  java
  • 将动态页面转成静态页面

    在App_Code文件下新建两个类:

    static_htm类用来处理将动态页面转化成静态页面。

    如下:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;

    /// <summary>
    /// static_htm 的摘要说明
    /// </summary>
    public class static_htm : System.Web.UI.Page {
     public static_htm()
     {
      //
      // TODO: 在此处添加构造函数逻辑
      //
     }

        protected override void Render(HtmlTextWriter writer)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter htmlw = new HtmlTextWriter(sw);
            //调用Render方法,把页面内容输出到StringWriter中

            base.Render(htmlw);
            htmlw.Flush();
            htmlw.Close();
            //获得页面内容
            string pageContent = sw.ToString();
            string path = Server.MapPath("~/CacheFile/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string pageUrl = StaticFileCacheModule.GetFileName(HttpContext.Current);
            if (!File.Exists(pageUrl))
            {
                //把页面内容保存到静态文件中
                using (StreamWriter stringWriter = File.CreateText(path + pageUrl))
                {
                    stringWriter.Write(pageContent);
                }
            }
            //将页面内容输出到浏览器
            Response.Write(pageContent);

        }


    }

    StaticFileCacheModule类用来处理哪些动态页面需要转化成静态页面的,

    如下:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.IO;

    /// <summary>
    /// StaticFileCacheModule 的摘要说明
    /// </summary>
    public class StaticFileCacheModule : IHttpModule
    {
     public StaticFileCacheModule()
     {
      //
      // TODO: 在此处添加构造函数逻辑
      //
            Console.WriteLine();
     }


        public void Dispose()
        {
            
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest); 
        }
        private void context_BeginRequest(object sender, EventArgs e)
        {

            HttpContext context = ((HttpApplication)sender).Context;

            //判断是否需要处理

            if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))
            {

                string fileUrl = "~/CacheFile/";

                string fileName = GetFileName(context);

                string filePath = context.Server.MapPath(fileUrl) + fileName;

                if (File.Exists(filePath))
                {

                    //如果静态缓存文件存在,直接返回缓存文件

                    context.RewritePath(fileUrl + fileName, false);

                }
               

            }
        }


        public static string GetFileName(HttpContext context)
        {

            //我们的缓存文件名由页面文件名加上查询字符串组成

            return context.Request.AppRelativeCurrentExecutionFilePath.ToLower()

                .Replace(".aspx", "").Replace("~/", "").Split('/')[context.Request.AppRelativeCurrentExecutionFilePath.ToLower()

                .Replace(".aspx", "").Replace("~/", "").Split('/').Length - 1]

                + context.Request.Url.Query.Replace("?", "__").Replace("&", "_") + ".html";

        }

    }

    配置Web.Config文件,如下:

    <httpModules>
       <add name="StaticFileCache"  type="StaticFileCacheModule"/>
     </httpModules>

    最后:

    希望要转成静态页面的动态页面继承static_htm:

    如:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : static_htm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           

        }

    }

    来源:http://blog.csdn.net/lxfjoy/article/details/6722147

  • 相关阅读:
    程序员常用英语词汇
    声明式编程与命令式编程
    vue 常用ui组件库
    Vue 组件之间传值
    vscode插件之背景插件(background)
    iconfont的使用
    CSS3 @font-face 规则
    CSS抗锯齿 font-smoothing 属性介绍
    new Image 读取宽高为0——onload
    js的for循环中出现异步函数,回调引用的循环值始终是最后的值
  • 原文地址:https://www.cnblogs.com/everyone/p/2801409.html
Copyright © 2011-2022 走看看