自动版本的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.
这个链接 - <一个href=\"http://stackoverflow.com/questions/118884/what-is-an-elegant-way-to-force-browsers-to-reload-cached-css-js-files\">What是一种优雅的方式迫使浏览器重新加载缓存的CSS / JS文件 - 提供Apache的解决方案 - 但我有点困惑如何可通过ASP.NET MVC
This link - What is an elegant way to force browsers 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 ?
也就是说,这样的链接出来这个链接等presumably使用URL重写或?
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>
THX
推荐答案
在面对这个问题,我写了一系列的围绕着的 UrlHelper
<$ C包装功能$ C>内容方法:
When faced with this problem I wrote a series of wrapper functions around the UrlHelper
's Content
method:
编辑:
继在评论中讨论下面我更新了这个code:
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>
<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.
这code使用当前程序集的版本号作为文件路径上的查询字符串参数是它发出。当我做一个更新的网站和内部版本号的增量,所以确实在该文件的查询参数,因此用户代理将重新下载该文件。
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屋!