首先iis注册.ghtml文件交给.net处理。
然后将需要生成ghtml的aspx文件通过这个函数处理,也就是生成静态文件,再多一步压缩
/**/ //// <summary> /// 在html目录下生成压缩html /// </summary> /// <param name="aspxPath">动态页面请求路径</param> /// <param name="urlPath">静态页面请求路径</param> public static void AspxToHtml( string aspxPath, string urlPath) { string filePath = HttpContext.Current.Server.MapPath(urlPath); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath.Substring(0,filePath.LastIndexOf("\\"))); } using (FileStream fs = new FileStream(filePath, FileMode.Create)) { using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress)) { using (StreamWriter sw = new StreamWriter(gz, Encoding.UTF8)) { HttpContext.Current.Server.Execute(aspxPath, sw,false); sw.Flush(); } } } }处理ghtml请求,浏览器支持gzip就直接写入文件,否则先解压内容再输出:
自己写个HttpModule,在BeginRequest事件中处理.ghtml请求 ,静态页嘛就模拟一下html的304处理
public class FreeModule : IHttpModule { // Init方法仅用于给期望的事件注册方法 public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } private void SetClientCaching(HttpResponse response, DateTime lastModified) { response.Cache.SetETag(lastModified.Ticks.ToString()); response.Cache.SetLastModified(lastModified); response.Cache.SetCacheability(HttpCacheability.Public); } // 处理BeginRequest 事件的实际代码 void context_BeginRequest(object sender, EventArgs e) { HttpApplication application = (HttpApplication)sender; HttpContext context = application.Context; HttpRequest request=context.Request; HttpResponse response = context.Response;
string path = context.Request.Path.ToLower(); string acceptEncoding = request.Headers["Accept-Encoding"];
bool accept = !string.IsNullOrEmpty(acceptEncoding)? acceptEncoding.ToLower().Contains("gzip") : false;
if ( path .Contains(".ghtml")) { string filePath = request.PhysicalApplicationPath + "/html" + path ; if (!File.Exists(filePath)) { throw new FileNotFoundException("找不到文件ghtml"); } DateTime writeTime = File.GetLastWriteTimeUtc(filePath); DateTime since; if (DateTime.TryParse(request.Headers["IfModifiedSince"],out since) && writeTime == since.ToUniversalTime() ) { response.StatusCode = 304; response.StatusDescription = "Not Modified"; } else
{
if (accept ) {
response.AppendHeader("Content-Encoding", "gzip");
response.TransmitFile(filePath);
}
else
{
response.Write(DezipText(filePath));// 解压ghtml文件
}
SetClientCaching(response, writeTime); response.End(); } } } public void Dispose() { } }
解压ghtml:
最后还有个解压ghtml函数
/**/ /// <summary> /// 解压text文件后返回str /// </summary> /// <param name="textPath">物理路径</param> /// <returns>文件字符串</returns> public static string DezipText( string textPath) { using (FileStream fs = File.OpenRead(textPath)) { GZipStream gz = new GZipStream(fs, CompressionMode.Decompress); StreamReader sr = new StreamReader(gz); return sr.ReadToEnd(); } }
转载于:https://www.cnblogs.com/cgy985/archive/2008/07/22/1248943.html