您的位置:首页 > 编程语言 > ASP

asp.net mvc3的静态化实现

2011-11-13 13:08 281 查看
静态化处理,可以大大提高客户的访问浏览速度,提高用户体验,同时也降低了服务器本身的压力。在asp.net mvc3中,可以相对容易地处理静态化问题,不用过多考虑静态网页的同步,生成等等问题。我提供这个方法很简单,就需要在需要静态化处理的Controller或Action上加一个Attribute就可以。下面是我写的一个生成静态文件的ActionFilterAttribute。

1 using System;

2 using System.IO;
3 using System.Text;
4 using System.Web;
5 using System.Web.Mvc;
6 using NLog;
7
8 /// <summary>
9 /// 生成静态文件
10 /// </summary>
11 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
12 public class GenerateStaticFileAttribute : ActionFilterAttribute
13 {
14 #region 私有属性
15
16 private static readonly Logger logger = LogManager.GetCurrentClassLogger();
17
18 #endregion
19
20 #region 公共属性
21
22 /// <summary>
23 /// 过期时间,以小时为单位
24 /// </summary>
25 public int Expiration { get; set; }
26
27 /// <summary>
28 /// 文件后缀名
29 /// </summary>
30 public string Suffix { get; set; }
31
32 /// <summary>
33 /// 缓存目录
34 /// </summary>
35 public string CacheDirectory { get; set; }
36
37 /// <summary>
38 /// 指定生成的文件名
39 /// </summary>
40 public string FileName { get; set; }
41
42 #endregion
43
44 #region 构造函数
45
46 /// <summary>
47 /// 默认构造函数
48 /// </summary>
49 public GenerateStaticFileAttribute()
50 {
51 Expiration = 1;
52 CacheDirectory = AppDomain.CurrentDomain.BaseDirectory;
53 }
54
55 #endregion
56
57 #region 方法
58
59 public override void OnResultExecuted(ResultExecutedContext filterContext)
60 {
61 var fileInfo = GetCacheFileInfo(filterContext);
62
63 if ((fileInfo.Exists && fileInfo.CreationTime.AddHours(Expiration) < DateTime.Now) || !fileInfo.Exists)
64 {
65 var deleted = false;
66
67 try
68 {
69 if (fileInfo.Exists)
70 {
71 fileInfo.Delete();
72 }
73
74 deleted = true;
75 }
76 catch (Exception ex)
77 {
78 logger.Error("删除文件:{0}发生异常:{1}", fileInfo.FullName, ex.StackTrace);
79 }
80
81 var created = false;
82
83 try
84 {
85 if (!fileInfo.Directory.Exists)
86 {
87 fileInfo.Directory.Create();
88 }
89
90 created = true;
91 }
92 catch (IOException ex)
93 {
94 logger.Error("创建目录:{0}发生异常:{1}", fileInfo.DirectoryName, ex.StackTrace);
95 }
96
97 if (deleted && created)
98 {
99 FileStream fileStream = null;
StreamWriter streamWriter = null;

try
{
var viewResult = filterContext.Result as ViewResult;
fileStream = new FileStream(fileInfo.FullName, FileMode.CreateNew, FileAccess.Write, FileShare.None);
streamWriter = new StreamWriter(fileStream);
var viewContext = new ViewContext(filterContext.Controller.ControllerContext, viewResult.View, viewResult.ViewData, viewResult.TempData, streamWriter);
viewResult.View.Render(viewContext, streamWriter);
}
catch (Exception ex)
{
logger.Error("生成缓存文件:{0}发生异常:{1}", fileInfo.FullName, ex.StackTrace);
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
}

if (fileStream != null)
{
fileStream.Close();
}
}
}
}
}

/// <summary>
/// 生成文件Key
/// </summary>
/// <param name="controllerContext">ControllerContext</param>
/// <returns>文件Key</returns>
protected virtual string GenerateKey(ControllerContext controllerContext)
{
var url = controllerContext.HttpContext.Request.Url.ToString();

if (string.IsNullOrWhiteSpace(url))
{
return null;
}

var th = new TigerHash();
var data = th.ComputeHash(Encoding.Unicode.GetBytes(url));
var key = Convert.ToBase64String(data, Base64FormattingOptions.None);
key = HttpUtility.UrlEncode(key);

return key;
}

/// <summary>
/// 获取静态的文件信息
/// </summary>
/// <param name="controllerContext">ControllerContext</param>
/// <returns>缓存文件信息</returns>
protected virtual FileInfo GetCacheFileInfo(ControllerContext controllerContext)
{
var fileName = string.Empty;

if (string.IsNullOrWhiteSpace(FileName))
{
var key = GenerateKey(controllerContext);

if (!string.IsNullOrWhiteSpace(key))
{
fileName = Path.Combine(CacheDirectory, string.IsNullOrWhiteSpace(Suffix) ? key : string.Format("{0}.{1}", key, Suffix));
}
}
else
{
fileName = Path.Combine(CacheDirectory, FileName);
}

return new FileInfo(fileName);
}

#endregion
}

如果大家对于生成的文件和目录有特殊的要求,那可以重写GetCacheFileInfo方法,比如按照日期生成目录等等更复杂的目录和文件结构。当然以上代码只是提供了生成静态页的方法,但是访问如何解决呢? 访问静态文件和规则就需要在HttpApplication的Application_BeginRequest实现了。首先可以设置需要静态化访问的路由地址以html结尾。下面的是一个用于首页的静态化访问的实现,很简单,当然你可以实现比较复杂的逻辑,比如根据文件时间来判断是否应该访问静态文件等等。

1 protected void Application_BeginRequest(object sender, EventArgs e)
2 {
3 StaticContentRewrite();
4 }
5
6 /// <summary>
7 /// 处理静态发布内容
8 /// </summary>
9 private void StaticContentRewrite()
{
if (Context.Request.FilePath == "/" || Context.Request.FilePath.StartsWith("/index.html", StringComparison.OrdinalIgnoreCase))
{
if (File.Exists(Server.MapPath("index.html")))
{
Context.RewritePath("index.html");
}
}18 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: