上一篇有提到利用IHttpModule和ResultFilter实现页面静态化功能。后来经过一些改动,将ResultFilter中要实现的功能全部转移到IHttpModule中来实现

Asp.Net MVC页面静态化功能实现一:利用IHttpModule和ResultFilter

1、改动后的自定义IHttpModule实现代码:

public class RouterHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += this.Application_BeginRequest; //注册事件
} private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string filePath = context.Request.FilePath;
string fileExtension = VirtualPathUtility.GetExtension(filePath);
//如果当前请求的不是资源文件、不是后台管理、请求中没有表单信息、静态页面存在,则返回静态页面
if (string.IsNullOrEmpty(fileExtension) && !filePath.ToLower().StartsWith("/admin") && context.Request.Form.Count.Equals())
{
string htmlPath = context.Request.HtmlFilePath();
if (File.Exists(htmlPath))
{
context.Response.WriteFile(htmlPath);
context.Response.End();
}
context.Response.Filter = new HttpResponseFilterWrapper(context.Response.Filter, context);
}
} public void Dispose() { }
}

这里只是在判断静态页面文件是否存在之后加了这样一段代码:context.Response.Filter = new HttpResponseFilterWrapper(context.Response.Filter, context);

2、HttpResponseFilterWrapper的实现代码

HttpResponseFilterWrapper的功能还跟以前一样,保存Filter中返回给客户端的html代码到服务器硬盘上

public class HttpResponseFilterWrapper : Stream
{
private Stream inner;
private HttpContext context;
public HttpResponseFilterWrapper(System.IO.Stream s, HttpContext context)
{
this.inner = s;
this.context = context;
} public override bool CanRead
{
get { return inner.CanRead; }
} public override bool CanSeek
{
get { return inner.CanSeek; }
} public override bool CanWrite
{
get { return inner.CanWrite; }
} public override void Flush()
{
inner.Flush();
} public override long Length
{
get { return inner.Length; }
} public override long Position
{
get{ return inner.Position; }
set{ inner.Position = value; }
} public override int Read(byte[] buffer, int offset, int count)
{
return inner.Read(buffer, offset, count);
} public override long Seek(long offset, System.IO.SeekOrigin origin)
{
return inner.Seek(offset, origin);
} public override void SetLength(long value)
{
inner.SetLength(value);
} public override void Write(byte[] buffer, int offset, int count)
{
if (!context.Response.StatusCode.Equals())
{
return;
}
inner.Write(buffer, offset, count);
//if (!context.HttpContext.Request.FilePath.ToLower().StartsWith("/admin"))
//当前请求不是后台管理;并且返回客户端是html才生成静态页面
if (!context.Request.FilePath.ToLower().StartsWith("/admin") && context.Response.ContentType.Equals("text/html"))
{
//静态页面保存路径信息
string htmlPath = context.Request.HtmlFilePath();
string direcHtmlPath = Path.GetDirectoryName(htmlPath);
if (!Directory.Exists(direcHtmlPath))
{
Directory.CreateDirectory(direcHtmlPath);
}
//获取返回客户端的html代码,并进行压缩处理
string htmlCode = System.Text.Encoding.UTF8.GetString(buffer);
string isZipHtml = WebConfigInfo.GetConfigValueByKey("IsCompressed");
//如果“IsCompressed”的值为空或0,则表示不压缩
if (!string.IsNullOrEmpty(isZipHtml) && !isZipHtml.Equals())
{
htmlCode = Regex.Replace(htmlCode, "^\\s*", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
htmlCode = Regex.Replace(htmlCode, "\\r\\n", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
htmlCode = Regex.Replace(htmlCode, "<!--*.*?-->", string.Empty, RegexOptions.Compiled | RegexOptions.Multiline);
}
//保存文件,这里不能用File.WriteAllText
File.AppendAllText(htmlPath, htmlCode);
}
}
}

3、确保静态页面数据的及时性

当在后台管理系统中,对某个栏目或某篇文章进行过增删改的操作时,要同时保证静态页面中的数据与数据库中的数据保持一致。

最初想到的实现方法是在静态页面中设定一个过期时间,但是公司所使用的框架中内容信息是通过异步加载进来的,所以保存的静态页面中不存<html><head><body>标签,所以就放弃了这种方法。因为项目时间关系,最后偷了一点懒,在后台管理中添加一个“重新发布”的功能,直接将之前生成的静态页面全部删除掉。

Asp.Net MVC页面静态化功能实现一:利用IHttpModule,摒弃ResultFilter的更多相关文章

  1. Asp.Net MVC页面静态化功能实现二:用递归算法来实现

    上一篇提到采用IHttpModule来实现当用户访问网站的时候,通过重新定义Response.Filter来实现将返回给客户端的html代码保存,以便用户下一次访问是直接访问静态页面. Asp.Net ...

  2. Asp.Net MVC页面静态化功能实现一:利用IHttpModule和ResultFilter

    由于公司现在所采用的是一套CMS内容管理系统的框架,所以最近项目中有一个需求提到要求实现页面静态化的功能.在网上查询了一些资料和文献,最后采用的是小尾鱼的池塘提供的 利用ResultFilter实现a ...

  3. ASP.NET MVC 页面静态化操作的思路

    本文主要讲述了在asp.net mvc中,页面静态化的几种思路和方法.对于网站来说,生成纯html静态页面除了有利于seo外,还可以减轻网站的负载能力和提高网站性能.在asp.net mvc中,视图的 ...

  4. 利用ResultFilter实现asp.net mvc 页面静态化

    为了提高网站性能.和网站的负载能力,页面静态化是一种有效的方式,这里对于asp.net mvc3 构架下的网站,提供一种个人认为比较好的静态话方式. 实现原理是通过mvc提供的过滤器扩展点实现页面内容 ...

  5. Asp.net Mvc 页面静态化

    http://www.cnblogs.com/gowhy/archive/2013/01/01/2841472.html

  6. Asp.net动态页面静态化之初始NVelocity模板引擎

    Asp.net动态页面静态化之初始NVelocity模板引擎 静态页面是网页的代码都在页面中,不须要运行asp,php,jsp,.net等程序生成client网页代码的网页,静态页面网址中一般不含&q ...

  7. MVC页面静态化

    MVC 页面静态化   最近工作需要,实现页面静态化,以前在ASP时代,都是FSO自己手动生成的. 新时代,MVC了,当然也要新技术,网上一搜,找到一种解决方案,是基于MVC3的,实现原理是通过mvc ...

  8. MVC 页面静态化

    最近工作需要,实现页面静态化,以前在ASP时代,都是FSO自己手动生成的. 新时代,MVC了,当然也要新技术,网上一搜,找到一种解决方案,是基于MVC3的,实现原理是通过mvc提供的过滤器扩展点实现页 ...

  9. 利用ResultFilter实现asp.net mvc3 页面静态化

    为了提高网站性能.和网站的负载能力,页面静态化是一种有效的方式,这里对于asp.net mvc3 构架下的网站,提供一种个人认为比较好的静态话方式. 实现原理是通过mvc提供的过滤器扩展点实现页面内容 ...

随机推荐

  1. X86在逻辑地址、线性地址、理解虚拟地址和物理地址

    参考:http://bbs.chinaunix.net/thread-2083672-1-1.html 本贴涉及的硬件平台是X86.假设是其他平台,不保证能一一对号入座.可是举一反三,我想是全然可行的 ...

  2. Linux系统下启动MySQL报错:Neither host &#39;localhost.localdomain&#39; nor &#39;localhost&#39; could be looked up with

    Linux系统下启动MySQL报错:Neither host 'localhost.localdomain' nor 'localhost' could be looked up with 摘要 Li ...

  3. 利用纯CSS3实现超立体的3D图片侧翻倾斜效果

    原文:利用纯CSS3实现超立体的3D图片侧翻倾斜效果 上午的时候我在jQuery论坛上看到网友分享的一款CSS3 3D图片侧翻倾斜特效,觉得效果非常棒,其实话说回来,这玩意儿的实现真的非常简单,主要是 ...

  4. ViewPager用法

    第一图:          页面中填充内容是随机关键词飞入和飞出动画效果,随后会更新,如今请先无视吧 ---2015-02-27--- 两年后最终更新了,网上都能搜到的,哎 无奈太懒http://bl ...

  5. HDU ACM 1007 Quoit Design 分而治之的方法,最近点

    意甲冠军:给n坐标点.半一对点之间的距离所需的距离最近. 分析:分而治之的方法,最近点. #include<iostream> #include<algorithm> #inc ...

  6. 如何使用Ubuntu打电话

    在这个视频,我们学习如何使用Ubuntu打电话.Ubuntu手机的很多用户谁是不是很熟悉. 特别是,他什么都无所谓的物理按键(菜单键.home纽带.回车键).然后用户如何控制手机它?Ubuntu手机凭 ...

  7. hdu1086(线段相交)

    题目意思: 给出n个线段,推断这n条线段中,线段相交的对数. http://acm.hdu.edu.cn/showproblem.php?pid=1086 题目分析: 此题主要写出推断线段相交的函数, ...

  8. shell变量赋值进阶

    首先,要理解shell中变量的3种赋值情况: unset 例子. unset a 空字符串, null 例子. a='' 非空,即不是unset,并且不是空字符串 例子: a=1 or a=b等 然后 ...

  9. 大约HR升级版的设计为组汇总

    该公司刚刚完成HR系统升级,系统从单一公司实现使用更多的公司使用变更.在一个月的时间升级,虽然很苦,但他们自己的系统架构的感觉获益,有以下的详细的见解: 一.MVC还是非常重要 系统框架是五年前用de ...

  10. 使用Visual Studio创建映像向导(Image Sprite)——Web Essential

    原版的:Creating Image Sprite in Visual Studio - Web Essential 译者注:有关图片精灵的信息请參阅http://baike.baidu.com/vi ...