ASP.NET MVC 扩展HtmlHelper类为 js ,css 资源文件添加版本号
写在前面
在项目部署当中会需要更新 css 文件或 js 等资源文件,为了避免由于浏览器缓存的原因无法加载新的 css 或 js ,一般的做法是在资源文件的后面加上一个版本号来解决,这样浏览器就会去服务器下载新的资源文件。
如果某个 css 文件被多个页面引用,那么我们就需要去每个页面一个一个的去修改,这样做的方式属于重复性的动作,而且有的时候还会漏掉需要修改的页面,所以我们就需要一个自动管理资源文件版本号的功能
先看效果
如何实现
通过扩展HemHelper 类,添加 为 js 和 css 文件处理的方法
public static class HtmlHelperExtension
{
/// <summary>
/// 自动为 Js 文件添加版本号
/// </summary>
/// <param name="html"></param>
/// <param name="contentPath"></param>
/// <returns></returns>
public static MvcHtmlString Script(this HtmlHelper html, string contentPath)
{
return VersionContent(html, "<script src=\"{0}\" type=\"text/javascript\"></script>", contentPath);
}
/// <summary>
/// 自动为 css 文件添加版本号
/// </summary>
/// <param name="html"></param>
/// <param name="contentPath"></param>
/// <returns></returns>
public static MvcHtmlString Style(this HtmlHelper html, string contentPath)
{
return VersionContent(html, "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\">", contentPath);
} private static MvcHtmlString VersionContent(this HtmlHelper html, string template, string contentPath)
{
var httpContenxt = html.ViewContext.HttpContext;
string hashValue = VersionUtils.GetFileVersion(httpContenxt.Server.MapPath(contentPath));
contentPath = UrlHelper.GenerateContentUrl(contentPath, httpContenxt) + "?v=" + hashValue;
return MvcHtmlString.Create(string.Format(template, contentPath));
} }
新建一个 VersionUtils 类来生成资源文件的版本号,下面的代码实现了计算文件的 hash 值作为版本号
public static class VersionUtils
{
public static Dictionary<string, string> FileHashDic = new Dictionary<string, string>();
public static string GetFileVersion(string filePath)
{
/*
* 生成版本号有三种方式
* 1. 将文件的将最后一次写入时间作为版本号 => File.GetLastWriteTime(filePath).ToString("yyyyMMddHHmmss");
* 2. 从配置文件中读取预先设定版本号 => ConfigurationManager.AppSettings["Js_CSS_Version"];
* 3. 计算文件的 hash 值
*/ string fileName = Path.GetFileName(filePath);
// 验证是否已计算过文件的Hash值,避免重复计算
if (FileHashDic.ContainsKey(fileName))
{
return FileHashDic[fileName];
}
else
{
string hashvalue = GetFileShaHash(filePath); //计算文件的hash值
FileHashDic.Add(fileName, hashvalue);
return hashvalue;
}
} private static string GetFileShaHash(string filePath)
{
string hashSHA1 = String.Empty;
//检查文件是否存在,如果文件存在则进行计算,否则返回空值
if (System.IO.File.Exists(filePath))
{
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
//计算文件的SHA1值
System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
Byte[] buffer = calculator.ComputeHash(fs);
calculator.Clear();
//将字节数组转换成十六进制的字符串形式
StringBuilder stringBuilder = new StringBuilder();
for (int i = ; i < buffer.Length; i++)
{
stringBuilder.Append(buffer[i].ToString("x2"));
}
hashSHA1 = stringBuilder.ToString();
}//关闭文件流
}
return hashSHA1;
}
}
如何使用
在View中的使用方式
@Html.Style("~/Content/table.css")
@Html.Style("~/Content/wxSite.css")
@Html.Script("~/Scripts/jquery-1.10.2.min.js")
参考文章
https://www.cnblogs.com/aehyok/archive/2012/11/17/2774500.html
ASP.NET MVC 扩展HtmlHelper类为 js ,css 资源文件添加版本号的更多相关文章
- [Asp.net Mvc]为js,css静态文件添加版本号
方式一: 思路 string version = ViewBag.Version; @Scripts.RenderFormat("<script type=\"text/ja ...
- [Asp.net Mvc]通过UrlHelper扩展为js,css静态文件添加版本号
写在前面 在app中嵌入h5应用,最头疼的就是缓存的问题,比如你修改了一个样式,或者在js中添加了一个方法,发布之后,并没有更新,加载的仍是缓存里面的内容.这个时候就需要清理缓存才能解决.但又不想让w ...
- ASP.NET MVC 扩展HtmlHelper类方法
1.扩展HtmlHelper类方法ShowPageNavigate 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- gulp实现打包js/css/img/html文件,并对js/css/img文件加上版本号
参考打包教程: http://www.cnblogs.com/tugenhua0707/p/4069769.html http://www.cnblogs.com/tugenhua0707/p/498 ...
- ASP.NET MVC Bundles 合并压缩(js css)
Chrome浏览器有并发的Http请求限制,Bundles可以将多个JS文件合并成一个文件并进行压缩,最终得到一个单文件的压缩包. 第一步:BundleConfig public class Bund ...
- 一点ASP.NET MVC Html.Helper类的方法
一点ASP.NET MVC Html.Helper类 这里就只写一个Html.ActionLink()和Html.DropdownList(). Html.ActionLink()里有三个参数,第一个 ...
- ASP.NET MVC扩展库
很多同学都读过这篇文章吧 ASP.NET MVC中你必须知道的13个扩展点,今天给大家介绍一个ASP.NET MVC的扩展库,主要就是针对这些扩展点进行.这个项目的核心是IOC容器,包括Ninject ...
- 构建工具是如何用 node 操作 html/js/css/md 文件的
构建工具是如何用 node 操作 html/js/css/md 文件的 从本质上来说,html/js/css/md ... 源代码文件都是文本文件,文本文件的内容都是字符串,对文本文件的操作其实就是对 ...
- Spring 加载类路径外的资源文件
原文:http://blog.csdn.net/gaofuqi/article/details/46417259 <bean id="propertyConfigurer" ...
随机推荐
- GridView上同时定义了 DataSource 和 DataSourceId
VS平台下ASP.NET网站的建立,我们常常要跟数据库打交道,获取数据库的信息,通过GridView控件进行显示,需要为GridView指定 DataSourceId或者DataSource,切忌不可 ...
- JAVA list集合两种去重方法
结果: 转载地址:http://geek.csdn.net/news/detail/127940
- 一句话引发的思考 - synchronized/super
https://blog.csdn.net/cool__wang/article/details/52459380#commentBox
- pycharm中的常用快捷键
查找 Ctrl + F 替换 Ctrl + R 注释 Ctrl + / 去掉注释 Ctrl + / Function Shortcut Use this shortcut to... Clos ...
- VMware workstation 设定开机引导等待时间
找到虚拟机磁盘文件所在的目录,编辑里面的扩展名为vmx文件,记事本即可操作,在末尾加入如下一行: bios.bootDelay = "20000" 这里的数字是毫秒,上面例子中的数 ...
- 13.8.8 div块 居中
<div style="border:1px solid blue;width:760px; height:410px; position:absolute; left:50%; to ...
- mysql 导入导出摘要
1.导入by数据文件. mysql>load data infile '文件路径' into table 表名 fields terminated by '字段分隔符' lines termin ...
- android触控,先了解MotionEvent(一)
http://my.oschina.net/banxi/blog/56421 这是我个人的看法,要学好android触控,了解MotionEvent是必要,对所用的MotionEvent常用的API要 ...
- 高性能 js -- 无阻塞加载脚本
参考: <<高性能JavaScript>> Nicbolas C. Zakas 著 javascript代码的下载和执行过程会阻塞浏览器的其他进程, 比如页面的绘制, 遇到&l ...
- 2018.10.05 NOIP模拟 相遇(dfs序+lca)
传送门 考虑到两条路径相交的条件: 设两条路径为a,ba,ba,b. 则要么aaa路径的lcalcalca在bbb上. 要么bbb路径的lcalcalca在aaa上. 因此我们维护两棵树. 分别支持路 ...