zoukankan      html  css  js  c++  java
  • 静态文件自动加版本号

    你还在为浏览器缓存而困扰么,快来看吧

     1 /// <summary>
     2     /// Auto versioning of custom static files.
     3     /// </summary>
     4     public static class HtmlHelperExtension
     5     {
     6         public static MvcHtmlString IncludeVersionedJs(this HtmlHelper helper, string filename)
     7         {
     8             string version = GetVersion(helper, filename);
     9             return MvcHtmlString.Create("<script type='text/javascript' src='" + filename + version + "'></script>");
    10         }
    11 
    12         public static MvcHtmlString IncludeVersionedCss(this HtmlHelper helper, string filename)
    13         {
    14             string version = GetVersion(helper, filename);
    15             return MvcHtmlString.Create("<link rel='stylesheet' href='" + filename + version + "'/>");
    16         }
    17 
    18         static string GetVersion(this HtmlHelper helper, string filename)
    19         {
    20             var context = helper.ViewContext.RequestContext.HttpContext;
    21 
    22             if (context.Cache[filename] == null)
    23             {
    24                 var physicalPath = context.Server.MapPath(filename);
    25                 var version = "?v=" + new System.IO.FileInfo(physicalPath).LastWriteTime.ToString("yyyyMMddHHmmss");
    26                 context.Cache.Add(physicalPath, version, null, DateTime.UtcNow.AddSeconds(60), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
    27                 context.Cache[filename] = version;
    28                 return version;
    29             }
    30             else
    31             {
    32                 return context.Cache[filename] as string;
    33             }
    34         }
    35 
    36         public static string GetFileWithVersion(this HtmlHelper helper, string filename)
    37         {
    38             return filename + GetVersion(helper, filename);
    39         }
    40     }
  • 相关阅读:
    oracle number数据类型
    codepage 和 charset
    嵌入式jetty的HTTP实现
    OpenCV For Java环境搭建与功能演示
    luogu P2783 有机化学之神偶尔会做作弊
    [国家集训队]稳定婚姻
    [SCOI2014]方伯伯运椰子
    [APIO2017]商旅
    luogu P1121 环状最大两段子段和
    [APIO/CTSC 2007]数据备份
  • 原文地址:https://www.cnblogs.com/hwisecn/p/6104061.html
Copyright © 2011-2022 走看看