生成事件并对.js和.css文件进行版本控制 [英] Build events and versioning .js and .css files
问题描述
我设置了一个MSBuild脚本,以缩小并合并我的JavaScript和CSS文件.我现在需要的是对它们进行版本控制的方法.你们目前如何处理.增量版本化文件并使用新文件名更新<script/>
标记的最佳方法是什么?
I have a MSBuild script set up to minify and combine my javascript and css files. What I need now is a way to version them. How are you guys currently handling this. What is the best way to incrementally version the file and update the <script/>
tag with the new file name?
推荐答案
我本来是建议使用资源表达式来包含AppSettings
中的版本标记,但经过测试后发现它仅在完整值时才有效服务器控件属性的名称.
I was originally going to suggest using resource expressions to include a version tag from AppSettings
, but after testing found that it only works if it is the entire value of a server control property.
AppSettings
的值可以由构建脚本针对每个发行版进行更新.可以是您喜欢的任何格式:
The AppSettings
value can be updated by the build script for every release; can be any format you like:
<appSettings>
<add key="versionTag" value="27" />
</appSettings>
作品:
<asp:Label runat="server" Text="<%$ AppSettings: versionTag %>" />
不起作用:
<link runat="server" rel="Stylesheet" type="text/css"
href='/css/site.css?v=<%$ AppSettings: versionTag %>' />
因此,我的实际建议是创建自己的控件,该控件读取version标签并将其包含在其输出中.这是我的CSS控件在页面上的外观(注意:服务器控件必须位于服务器端表单内,即使它可能会显示在head
元素内):
So my actual recommendation is to create your own controls that read the version tag and include that in their output. Here's how my CSS control looks on a page (NOTE: server controls must be inside the server-side form, even though it may render inside the head
element):
...
<form id="form1" runat="server">
<my:Stylesheet runat="server" Url="~/css/site.css" />
</form>
...
我的样式表控件的代码:
The code for my Stylesheet control:
namespace MyNamespace
{
using System;
using System.ComponentModel;
using System.Configuration;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
/// <summary>
/// Outputs a CSS stylesheet link that supports versionable caching via a
/// build-specific query parameter.
/// </summary>
[
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.LinkDemand,
Level = AspNetHostingPermissionLevel.Minimal),
DefaultProperty("Href"),
ToolboxData(@"<{0}:Stylesheet runat=""server"" />")
]
public class Stylesheet : WebControl
{
private static string versionTag = Stylesheet.GetVersionTag();
/// <summary>
/// Gets or sets the stylesheet URL.
/// </summary>
public string Href
{
get
{
return this.ViewState["Href"] as string;
}
set
{
this.ViewState["Href"] = value;
}
}
/// <summary>
/// Raises the PreRender event.
/// </summary>
/// <param name="e">Contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
HtmlLink link = new HtmlLink();
link.Href = String.Format(
"{0}?v={1}",
this.Page.ResolveUrl(this.Href),
HttpUtility.UrlEncode(Stylesheet.versionTag));
if (!Stylesheet.HeadContainsLinkHref(this.Page, link.Href))
{
link.Attributes["type"] = "text/css";
link.Attributes["rel"] = "Stylesheet";
this.Page.Header.Controls.Add(link);
}
}
/// <summary>
/// Generates content to be rendered on the client.
/// </summary>
/// <param name="writer">Receives the server control content.</param>
protected override void Render(HtmlTextWriter writer)
{
// Do nothing.
}
/// <summary>
/// Retrieves the script version tag for this build.
/// </summary>
/// <returns>Returns the script version tag.</returns>
private static string GetVersionTag()
{
string tag = ConfigurationManager.AppSettings["versionTag"];
if (String.IsNullOrEmpty(tag))
{
tag = "1";
}
return tag;
}
/// <summary>
/// Determines if the page's <c>head</c> contains a <c>link</c> tag
/// with a matching <c>href</c> attribute value.
/// </summary>
/// <param name="thePage">The Page to be tested.</param>
/// <param name="href">The <c>href</c> URL to be matched.</param>
/// <returns>Returns true if a matching link is already part of the
/// page <c>head</c> or false otherwise.</returns>
public static bool HeadContainsLinkHref(Page thePage, string href)
{
if (thePage == null)
{
throw new ArgumentNullException("thePage");
}
foreach (Control control in thePage.Header.Controls)
{
if ((control is HtmlLink) &&
(control as HtmlLink).Href == href)
{
return true;
}
}
return false;
}
}
}
HTH.
这篇关于生成事件并对.js和.css文件进行版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!