在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)
{
}
}