I would like to override Url.Content to append a query string parameter to the resulting string on Url.Content.
The reason being, I have a web application that I develop, and with each release, users must clear their cache to get the new css and js. A solution for this is to append a version number to the querystring to force loading of the new version.
A working solution is as follows:
@{ var version = "?v=" + ViewBag.VersionNumber; }
<head>
<link href="@Url.Content("~/ux/css/base.css")@version" rel="stylesheet" type="text/css" />
</head>
Version is set in a config file so with each release, the version is updated. I would like this to be more automatic though, as currently any time a new css reference is added, we must remember to add @version to the string. An extension method the returns the path with the version number already appended would be perfect.
Also, if anyone knows who I could make changing the version number automatic with TFS check-ins or compiles that would be really useful too.
You could do something like this:
public static string VersionedContent(this UrlHelper urlHelper, string contentPath)
{
string result = urlHelper.Content(contentPath);
var versionService = Engine.IocService.GetInstance<IVersionService>();
string tag = versionService.GetVersionTag();
if (result.Contains('?'))
{
result += "&v="+tag;
}
else
{
result += "?v="+tag;
}
return result;
}
Version Service could look something like this:
public class VersionService : IVersionService
{
string _versionTag;
public VersionService()
{
_versionTag = Assembly.GetExecutingAssembly().GetName().Version.ToString();
_versionTag = _versionTag.Replace('.', '-');
}
#region IVersionedContentService Members
public string GetVersionTag()
{
return _versionTag;
}
#endregion
}
You might want to take a look at cassette
* EDIT * For autom. build numbers with TFS, check out: automatic-assembly-file-version-numbering-in-tfs-2010
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.