我通过将最后修改的时间戳记作为查询参数解决了这个问题。
我用扩展方法做了这个,并在我的CSHTML文件中使用它。 注意:该实现将时间戳记缓存1分钟,所以我们不会对磁盘造成太大的影响。
这里是扩展方法:
public static class JavascriptExtension {
public static MvcHtmlString IncludeVersionedJs(this HtmlHelper helper, string filename) {
string version = GetVersion(helper, filename);
return MvcHtmlString.Create("<script type='text/javascript' src='" + filename + version + "'></script>");
}
private static string GetVersion(this HtmlHelper helper, string filename)
{
var context = helper.ViewContext.RequestContext.HttpContext;
if (context.Cache[filename] == null) {
var physicalPath = context.Server.MapPath(filename);
var version = "?v=" +
new System.IO.FileInfo(physicalPath).LastWriteTime
.ToString("yyyyMMddhhmmss");
context.Cache.Add(physicalPath, version, null,
DateTime.Now.AddMinutes(1), TimeSpan.Zero,
CacheItemPriority.Normal, null);
context.Cache[physicalPath] = version;
return version;
}
else {
return context.Cache[filename] as string;
}
}
然后在CSHTML页:
@Html.IncludeVersionedJs("/MyJavascriptFile.js")
在渲染HTML,这会显示为:
<script type='text/javascript' src='/MyJavascriptFile.ks?20111129120000'></script>
类似的问题,但仍然不同。 – Edgar 2010-04-13 14:43:44