天天看点

mvc生成静态html页面,MVC 页面静态化

原标题:MVC 页面静态化

最近工作需要,实现页面静态化,以前在ASP时代,都是FSO自己手动生成的。

新时代,MVC了,当然也要新技术,网上一搜,找到一种解决方案,是基于MVC3的,实现原理是通过mvc提供的过滤器扩展点实现页面内容的文本保存。

基本原理为:

1.为需要静态化的Action上加一个自定义的实现了IResultFilter的Attribute(具体见下文)

2.添加一条路由规则,实现访问静态页面,把它放在原有路由的前面

//http://product/123.html

routes.MapRoute(

"HTMLDefault", // Route name

"product/{id}.html", // URL with parameters

new { controller = "Home", action = "ProductDetails", id = 0 } // Parameter defaults

);

(准备应用在项目中做如下规划:管事员后台修改/删除产品,同时修改/删除其对应页面,添加无须创建页面,因为用户访问时会自动创建)

另外,原文下有同学反应

不会代码的人

因为每次只能写入一定数量的文字,如果过大就会再次写入,再次写入的话,楼主写了下面这判断:

if (File.Exists(p))

{

File.Delete(p);

}

存在就给删除了,所以只写入最后的一些文字

目前网页没太大,所以具体情况不清楚,用到再说了哈。

实现效果如下:

mvc生成静态html页面,MVC 页面静态化
mvc生成静态html页面,MVC 页面静态化

但为了达到以上效果,虽然照般教程,但还是无法实现,原因就是我用的VS2012,MVC4,web.config(网站根目录下)有所改变,经过对比

发现:

只要在system.webServer结点下添加一条

即可,否则,因为你路由设置了静态页面HTML,它会提示你找不到文件,404错误,切记切记。

以下为转载的文章,方便学习:

public class StaticFileWriteFilterAttribute : FilterAttribute, IResultFilter

{

public void OnResultExecuted(ResultExecutedContext filterContext)

{

}

public void OnResultExecuting(ResultExecutingContext filterContext)

{

filterContext.HttpContext.Response.Filter = new StaticFileWriteResponseFilterWrapper(filterContext.HttpContext.Response.Filter, filterContext);

}

class StaticFileWriteResponseFilterWrapper : System.IO.Stream

{

private System.IO.Stream inner;

private ControllerContext context;

public StaticFileWriteResponseFilterWrapper(System.IO.Stream s, ControllerContext 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)

{

inner.Write(buffer, offset, count);

try

{

string p = context.HttpContext.Server.MapPath(HttpContext.Current.Request.Path);

if (Path.HasExtension(p))

{

string dir = Path.GetDirectoryName(p);

if (!Directory.Exists(dir))

{

Directory.CreateDirectory(dir);

}

if (File.Exists(p))

{

File.Delete(p);

}

File.AppendAllText(p, System.Text.Encoding.UTF8.GetString(buffer));

}

}

catch (Exception ex)

{

}

}

}

}

我们的类StaticFileWriteFilterAttribute实现了IResultFilter,这个接口有两个方法,OnResultExecuted和OnResultExecuting,其中OnResultExecuting是在controller中的action代码执行完毕后,但viewresult执行之前(即页面内容生成之前)执行;OnResultExecuted方法是在viewresult执行之后(即页面内容生成之后)执行。

我们在页面生成之前将StaticFileWriteResponseFilterWrapper类注册给Response对象的Filter属性,这里使用包装类可以在没有副作用的情况下注入页面内容静态化的代码,对于处理业务逻辑的action是透明的。

使用方式:

全局注册:GlobalFilters.Filters.Add(new StaticFileWriteFilterAttribute );

单独controller注册

public class MyController:Controller

{

[StaticFileWriteFilter]

public ActionResult MyAction

{

}

}

在Global.asax中的RegisterRoutes 添加路由

routes.MapRoute(

"HTMLDefault", // Route name

"Subject/Index/{ArticleId}_{PageIndex}.html", // URL with parameters

new { controller = "Subject", action = "Index", ArticleId = 0, PageIndex=0 } // Parameter defaults

);

例如用户访问http://localhost:3509/Home/About.html如果不存在此静态文件 则访问action,action访问后则自动生成了此html文件,下次用户访问这个地址,就是访问静态文件了,不会进入action代码了。