我通过添加最后修改的时间戳作为脚本的查询参数来解决此问题。
我使用扩展方法进行了此操作,并在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("MMddHHmmss")}"; context.Cache.Add(filename, version, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.Normal, null); return version; } else { return context.Cache[filename] as string; } }}然后在CSHTML页面中:
@Html.IncludeVersionedJs("/MyJavascriptFile.js")在呈现的HTML中,其显示为:
<script type='text/javascript' src='https://www.mshxw.com/skin/sinaskin/image/nopic.gif'></script>



