簡體   English   中英

如何在CSS文件中動態添加版本?

[英]how to dynamically add the version in the css file?

如何使用編譯ASP或mb javascript在css文件(例如:main.css?v = 123129401278)中動態添加版本。 這是解決緩存問題

我曾經為此使用ScriptManager 我檢測到ModifiedDateTime並將其添加到文件中,因此無法將其緩存

這是完整的代碼

<asp:ScriptManager runat="server" EnablePageMethods="true" EnablePartialRendering="true"
        ID="sm" OnResolveScriptReference="sm_ResolveScriptReference">
     <Scripts>
        <asp:ScriptReference Path="~/scripts/jquery-1.4.2.min.js" /> 
    </Scripts>
</asp:ScriptManager>

然后

 protected void sm_ResolveScriptReference(object sender, ScriptReferenceEventArgs e)
      {
        if (!String.IsNullOrEmpty(e.Script.Path))
        {
            e.AddVersionToScriptPath(Server.MapPath(e.Script.Path));
        }
    }


public static void AddVersionToScriptPath(this System.Web.UI.ScriptReferenceEventArgs scrArg, string fullpath)
    {
        string scriptpath = scrArg.Script.Path;
        var fn = GetFileWriteTime(fullpath);
        if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["minifyJS"]) && ConfigurationManager.AppSettings["minifyJS"] == "1")
            scriptpath = scriptpath.Contains(".min.") ? scriptpath : scriptpath.Replace(".js", ".min.js");
        scrArg.Script.Path = String.Format("{0}?{1}", scriptpath, fn);
    }

public static string GetFileWriteTime(string fullpath)
    {
        string fw = "";
        if (File.Exists(fullpath))
        {
            FileInfo fi = new FileInfo(fullpath);
            fw += fi.LastWriteTime.ToString("yyMMddhhmm");
        }

        return fw;
    }

嘗試這個:

<link rel="stylesheet" type="text/css" href="http://mysite/style.css?id="+generateUUID()>

GenrateUUID

function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = (d + Math.random()*16)%16 | 0;
    d = Math.floor(d/16);
    return (c=='x' ? r : (r&0x7|0x8)).toString(16);
});
  return uuid;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM