在 ASP.NET MVC 中为 CSS/JS 文件自动版本控制? [英] Auto-versioning in ASP.NET MVC for CSS / JS Files?
问题描述
我已经阅读了很多关于如何自动版本化 CSS/JS 文件的文章 - 但这些文章都没有真正提供一种在 ASP.NET MVC 中执行此操作的优雅方法.
I have read lots of article on how to auto-version your CSS/JS files - but none of these really provide an elegant way to do this in ASP.NET MVC.
此链接 - 如何强制浏览器重新加载缓存的 CSS/JS 文件? - 为 Apache 提供了解决方案 - 但我有点困惑如何通过 ASP.NET MVC 实现?
This link - How to force browser to reload cached CSS/JS files? - provides a solution for Apache - but I'm a little confused how this could be implemented via ASP.NET MVC ?
谁能提供一些如何在 IIS7 和 ASP.NET MVC 上执行此操作的建议 - 以便 CSS/JS 文件自动在 URL 中插入版本号而不更改文件的位置?
Would anyone be able to provide some advice how to do this on IIS7 and ASP.NET MVC - so that CSS/JS files automatically have a version number inserted in the URL without changing the location of the file ?
也就是说,链接出来的链接大概是使用 URL Rewrite 或 ?
That is, so links come out link this etc presumably using the URL Rewrite or ?
<link rel="stylesheet" href="/css/structure.1194900443.css" type="text/css" />
<script type="text/javascript" src="/scripts/prototype.1197993206.js"></script>
谢谢
推荐答案
当遇到这个问题时,我围绕 UrlHelper
的 Content
方法编写了一系列包装函数:
When faced with this problem I wrote a series of wrapper functions around the UrlHelper
's Content
method:
根据下面评论中的讨论,我更新了此代码:
Following the discussions in the comments below I updated this code:
public static class UrlHelperExtensions
{
private readonly static string _version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
private static string GetAssetsRoot()
{
string root = ConfigurationManager.AppSettings["AssetsRoot"];
return root.IsNullOrEmpty() ? "~" : root;
}
public static string Image(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("{0}/v{2}/assets/img/{1}", GetAssetsRoot(), fileName, _version));
}
public static string Asset(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("{0}/v{2}/assets/{1}", GetAssetsRoot(), fileName, _version));
}
public static string Stylesheet(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("{0}/v{2}/assets/css/{1}", GetAssetsRoot(), fileName, _version));
}
public static string Script(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("{0}/v{2}/assets/js/{1}", GetAssetsRoot(), fileName, _version));
}
}
将这些函数与以下 rewrite
规则结合使用应该可以:
Using these functions in conjunction with the following rewrite
rule should work:
<rewrite>
<rules>
<rule name="Rewrite assets">
<match url="^v(.*?)/assets/(.*?)" />
<action type="Rewrite" url="/assets/{R:2}" />
</rule>
</rules>
</rewrite>
本文讨论如何在 IIS7 上创建重写规则.
This article discusses how to create rewrite rules on IIS7.
此代码使用当前程序集的版本号作为它发出的文件路径上的查询字符串参数.当我更新站点并且内部版本号增加时,文件上的 querystring 参数也会增加,因此用户代理将重新下载文件.
This code uses the version number of the current assembly as a query string parameter on the file path's it emits. When I do an update to the site and the build number increments, so does the querystring parameter on the file, and so the user agent will re-download the file.
这篇关于在 ASP.NET MVC 中为 CSS/JS 文件自动版本控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!