在ASP.NET MVC中自动版本化CSS/JS? [英] Autoversioning CSS/JS in ASP.NET MVC?
问题描述
所以我正在阅读此stackoverflow帖子关于ASP.NET MVC中CSS/JS文件中的自动版本控制",并想知道这样做的最佳"策略是什么.
So I was reading this stackoverflow post about "autoversioning" in ASP.NET MVC for CSS/JS files and was wondering what the "best" strategy is to do this.
提供的解决方案会插入一个程序集编号-这意味着每次发布时-都会更改每个不理想的单文件,因为如果您仅对1个* .css或* .js进行了修改,它将更改每个文件.
The solution provided inserts an assembly number - which means everytime you publish - it will change EVERY SINGLE file which is not ideal because if you make modifications to just 1 *.css or *.js then it will change each and every file.
1)如何仅针对单个文件"完成此操作,而不是使用带有修改日期或IIS7上的某些内容的站点范围内的程序集?
1) How can it be done just for "single files" instead of using site wide assembly using modification date or something on IIS7 ?
2)另外,如果我有某种静态"资产,例如- http://static .domain.com/js/123.js -如果有人将静态链接集成到他们的网站上,我该如何使用rewrite发送最新文件用于请求?
2) Also if I have some sort of "static" asset like - http://static.domain.com/js/123.js - how can I use rewrite to send the latest file for a request if someone has integrated this static link onto their site ?
即 http://static.domain.com/js/123.js 是链接,当有请求时-检查并发送最新文件?
i.e. http://static.domain.com/js/123.js is the link and when a request comes for this - check and send latest file ?
推荐答案
我解决此问题的方法是将自动版本化添加到 AssemblyInfo.cs 文件中的MVC项目中,如下所示:
The way I solved this problem was to add autoversioning to my MVC project in the AssemblyInfo.cs file like so:
Change
[assembly: AssemblyVersion("1.0.0.0")]
to
[assembly: AssemblyVersion("1.0.*")]
这意味着每次构建项目时,它都会有一个新的程序集版本,该版本高于上一个版本.现在您有了唯一的版本号.
然后,我创建了一个UrlHelperExtension类,该类将在需要时帮助我获取此信息:
I then created an UrlHelperExtension class that will help me get this information when I need it in my views:
public static class UrlHelperExtensions
{
public static string ContentVersioned(this UrlHelper self, string contentPath)
{
string versionedContentPath = contentPath + "?v=" + Assembly.GetAssembly(typeof(UrlHelperExtensions)).GetName().Version.ToString();
return self.Content(versionedContentPath);
}
}
您现在可以通过以下方式轻松地向视图中添加版本号:
You can now easily add a version number to your views in the following manner:
<link href="@Url.ContentVersioned("style.css")" rel="stylesheet" type="text/css" />
在查看页面源代码时,您现在会看到类似
When viewing your page source you will now have something that looks like
<link href="style.css?v=1.0.4809.30029" rel="stylesheet" type="text/css" />
这篇关于在ASP.NET MVC中自动版本化CSS/JS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!