版本CSS文件与查询字符串#1呢? [英] Versioning CSS files with a query string like Stackoverflow does?

查看:127
本文介绍了版本CSS文件与查询字符串#1呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你看一下Stackoverflow.com的源代码,你会看到引用他们的css文件是:

If you look at Stackoverflow.com's source you'll see the reference to their css file is:

<link href="/Content/all.min.css?v=2383" rel="stylesheet" type="text/css" />

这是怎么做的,使他们能够通过传递查询字符串的版本,并有正确的CSS文件中担任了?

How is this done so they can pass a version via query string and have the correct CSS file served up?

推荐答案

这( PHP的例子)文章解释你它背后的想法。基本上,可以happend的修改过的文件中的最后时间的时间戳。这样,每次你改变你的CSS,查询字符串会改变,逼迫浏览器下载的新版本。这是有效的CSS和JS文件。

This (PHP example) article explain you the idea behind it. Basically, you could happend the timestamp of the last time you modified the file. This way everytime you change your CSS, the querystring will change and "forcing" the browser to download the new version. This is valid for both CSS and JS files.

一个ASP.NET示例是这样的:

A ASP.NET sample is this:

public static string GetBreaker(string fileName)
{
    string cacheBreaker = null;
    try
    {
        if (fileName.StartsWith("~"))
        {
            fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName.Remove(0));
        }
        cacheBreaker = File.GetLastWriteTime(fileName).ToFileTime().ToString();
    }
    catch { }

    return string.IsNullOrEmpty(cacheBreaker) ? string.Empty : string.Format("?cachebreaker={0}", cacheBreaker);
}

和您以这种方式调用此方法在你的母版:

And you call this method in your MasterPage in this way:

<link href="<%= this.ResolveClientUrl("~/CSS/style.css") %><%=CacheBreaker.GetBreaker("~/CSS/style.css") %>"
            rel="stylesheet" type="text/css" />

这篇关于版本CSS文件与查询字符串#1呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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