Prevent scripts from being cached programmatically

后端 未结 6 1278
面向向阳花
面向向阳花 2021-01-23 16:23

I would like to ask if there is a way to prevent Firefox from caching scripts (.js files).

I have a project (ASP.Net Web App) with caching issue on firefox. When I first

6条回答
  •  情歌与酒
    2021-01-23 16:51

    1) Install IIS module: http://www.iis.net/download/urlrewrite

    2) Put this in web.config into section:

     
        
          
            
            
          
          
            
            
          
        
      
    

    3) Write this helper function (into some global class)

    public static string PutCSS(string filepath)
    {
        FileInfo f = new FileInfo(HttpContext.Current.Server.MapPath(filepath));
        string timestamp = f.LastWriteTimeUtc.Year.ToString() + f.LastWriteTimeUtc.Month.ToString() + f.LastWriteTimeUtc.Day.ToString() + f.LastWriteTimeUtc.Hour.ToString() + f.LastWriteTimeUtc.Minute.ToString() + f.LastWriteTimeUtc.Second.ToString() + f.LastWriteTimeUtc.Millisecond.ToString();
        return "\n";
    }
    
    public static string PutJS(string filepath)
    {
        FileInfo f = new FileInfo(HttpContext.Current.Server.MapPath(filepath));
        string timestamp = f.LastWriteTimeUtc.Year.ToString() + f.LastWriteTimeUtc.Month.ToString() + f.LastWriteTimeUtc.Day.ToString() + f.LastWriteTimeUtc.Hour.ToString() + f.LastWriteTimeUtc.Minute.ToString() + f.LastWriteTimeUtc.Second.ToString() + f.LastWriteTimeUtc.Millisecond.ToString();
        return "\n";
    }
    

    4) Instead of:

    
    
    

    Use:

    <%= global.PutCSS("Styles/style.css")%>
    <%= global.PutCSS("Javascript/userscript.css")%>
    

提交回复
热议问题