强制浏览器在 asp.net 应用程序中获取最新的 js 和 css 文件 [英] force browsers to get latest js and css files in asp.net application

查看:23
本文介绍了强制浏览器在 asp.net 应用程序中获取最新的 js 和 css 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有些浏览器会缓存 js 和 css 文件,除非您强制刷新它们,否则无法刷新它们.什么是最简单的方法.

Some browsers cache js and css files, failing to refresh them unless you force them to. What's the easiest way.

我刚刚实施了这个似乎有效的解决方案.

I just implemented this solution that seems to work.

在页面上声明一个版本变量

Declare a version variable on your page

  public string version { get; set; }

从 web.config key 获取版本号

Get the version number from web.config key

 version = ConfigurationManager.AppSettings["versionNumber"];

在您的 aspx 页面中,像这样调用 javascript 和样式表

In your aspx page make the calls to javascript and stylesheets like so

<script src="scripts/myjavascript.js?v=<%=version %>" type="text/javascript"></script>
<link href="styles/mystyle.css?v=<%=version %>" rel="stylesheet" type="text/css" />

因此,如果您在 web.config 中将版本从 1.0 设置为 1.1,您的浏览器将下载最新文件,这有望为您和您的用户节省一些麻烦.

So if you set the version = 1.1 from 1.0 in your web.config your browser will download the latest files which will hopefully save you and your users some frustration.

是否有其他更好的解决方案,或者这是否会导致网站出现任何不可预见的问题?

Is there another solution that works better, or will this cause any unforeseen issues for a website?

推荐答案

我通过将最后修改的时间戳作为查询参数添加到脚本中解决了这个问题.

I solved this by tacking a last modified timestamp as a query parameter to the scripts.

我使用扩展方法完成了此操作,并在我的 CSHTML 文件中使用了它.注意:此实现会将时间戳缓存 1 分钟,因此我们不会对磁盘​​造成太大的冲击.

I did this with an extension method, and using it in my CSHTML files. Note: this implementation caches the timestamp for 1 minute so we don't thrash the disk quite so much.

这里是扩展方法:

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 页面中:

And then in the CSHTML page:

 @Html.IncludeVersionedJs("/MyJavascriptFile.js")

在呈现的 HTML 中,这显示为:

In the rendered HTML, this appears as:

 <script type='text/javascript' src='/MyJavascriptFile.js?20111129120000'></script>

这篇关于强制浏览器在 asp.net 应用程序中获取最新的 js 和 css 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆