如何通过asp.net-mvc在版本信息中嵌入静态内容实现版本控制?

2026-03-30 12:421阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计524个文字,预计阅读时间需要3分钟。

如何通过asp.net-mvc在版本信息中嵌入静态内容实现版本控制?

我的静态内容缓存存在客户端问题(静态内容如js、css、jpeg、gif等)。并且客户端的我理解是我的开发机器大部分时间。因此,页面经常抛出脚本错误,无法正确显示。我不是Rails开发者。

我的静态内容缓存在客户端上有问题(静态我的意思是js,css,jpeg,gif等). (并且客户端我的意思是我的开发机器大部分时间).

因此,页面要么抛出脚本错误,要么无法正确显示.我不是Rails开发人员,但我及时读了几本关于它的书.我记得很清楚的一点是,它会在包含文件的末尾添加一些魔术版本号,因此它就变成了

如何通过asp.net-mvc在版本信息中嵌入静态内容实现版本控制?

<script src="~/Scripts/Invoice.js?201112091712" type="text/javascript"></script>

如果您修改该内容文件,它会生成一个新的版本号,因此它会生成一个不同的include语句,因此,客户端认为它是一个新内容,并且在不检查其缓存的情况下加载它.

asp.net-mvc 3& IIS 7支持这个,或者你知道任何模仿这种行为的工具吗?

谢谢,Hazım

我已经在我的一个项目中完成了这项工作,如果您喜欢,请随时使用我的助手:

public static class VersionedContentExtensions { public static MvcHtmlString VersionedScript(this HtmlHelper html, string file) { return VersionedContent(html, "<script src=\"{0}\" type=\"text/javascript\"></script>", file); } public static MvcHtmlString VersionedStyle(this HtmlHelper html, string file) { return VersionedContent(html, "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\">", file); } private static MvcHtmlString VersionedContent(this HtmlHelper html, string template, string file) { string hash = HttpContext.Current.Application["VersionedContentHash_" + file] as string; if (hash == null) { string filename = HttpContext.Current.Server.MapPath(file); hash = GetMD5HashFromFile(filename); HttpContext.Current.Application["VersionedContentHash_" + file] = hash; } return MvcHtmlString.Create(string.Format(template, file + "?v=" + hash)); } private static string GetMD5HashFromFile(string fileName) { FileStream file = new FileStream(fileName, FileMode.Open); MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } }

像这样使用它们:

@Html.VersionedScript("/Scripts/sccat.core.js")

本文共计524个文字,预计阅读时间需要3分钟。

如何通过asp.net-mvc在版本信息中嵌入静态内容实现版本控制?

我的静态内容缓存存在客户端问题(静态内容如js、css、jpeg、gif等)。并且客户端的我理解是我的开发机器大部分时间。因此,页面经常抛出脚本错误,无法正确显示。我不是Rails开发者。

我的静态内容缓存在客户端上有问题(静态我的意思是js,css,jpeg,gif等). (并且客户端我的意思是我的开发机器大部分时间).

因此,页面要么抛出脚本错误,要么无法正确显示.我不是Rails开发人员,但我及时读了几本关于它的书.我记得很清楚的一点是,它会在包含文件的末尾添加一些魔术版本号,因此它就变成了

如何通过asp.net-mvc在版本信息中嵌入静态内容实现版本控制?

<script src="~/Scripts/Invoice.js?201112091712" type="text/javascript"></script>

如果您修改该内容文件,它会生成一个新的版本号,因此它会生成一个不同的include语句,因此,客户端认为它是一个新内容,并且在不检查其缓存的情况下加载它.

asp.net-mvc 3& IIS 7支持这个,或者你知道任何模仿这种行为的工具吗?

谢谢,Hazım

我已经在我的一个项目中完成了这项工作,如果您喜欢,请随时使用我的助手:

public static class VersionedContentExtensions { public static MvcHtmlString VersionedScript(this HtmlHelper html, string file) { return VersionedContent(html, "<script src=\"{0}\" type=\"text/javascript\"></script>", file); } public static MvcHtmlString VersionedStyle(this HtmlHelper html, string file) { return VersionedContent(html, "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\">", file); } private static MvcHtmlString VersionedContent(this HtmlHelper html, string template, string file) { string hash = HttpContext.Current.Application["VersionedContentHash_" + file] as string; if (hash == null) { string filename = HttpContext.Current.Server.MapPath(file); hash = GetMD5HashFromFile(filename); HttpContext.Current.Application["VersionedContentHash_" + file] = hash; } return MvcHtmlString.Create(string.Format(template, file + "?v=" + hash)); } private static string GetMD5HashFromFile(string fileName) { FileStream file = new FileStream(fileName, FileMode.Open); MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } }

像这样使用它们:

@Html.VersionedScript("/Scripts/sccat.core.js")