37 lines
1.3 KiB
C#
37 lines
1.3 KiB
C#
using System;
|
|
using System.Web.Optimization;
|
|
using System.IO;
|
|
using System.Web.Hosting;
|
|
|
|
namespace Weee.Extensions
|
|
{
|
|
internal static class BundleExtensions
|
|
{
|
|
/// <summary>
|
|
/// Versioning for Bundles
|
|
/// Source: H-Dog's answer https://stackoverflow.com/questions/15005481/mvc4-stylebundle-can-you-add-a-cache-busting-query-string-in-debug-mode
|
|
/// </summary>
|
|
/// <param name="sb"></param>
|
|
/// <returns></returns>
|
|
public static Bundle WithLastModifiedToken(this Bundle sb)
|
|
{
|
|
sb.Transforms.Add(new LastModifiedBundleTransform());
|
|
return sb;
|
|
}
|
|
public class LastModifiedBundleTransform : IBundleTransform
|
|
{
|
|
public void Process(BundleContext context, BundleResponse response)
|
|
{
|
|
foreach (var file in response.Files)
|
|
{
|
|
// require some exception handle here to avoid illegal characters in the filename
|
|
if (!file.IncludedVirtualPath.Contains("?v=")) {
|
|
var lastWrite = File.GetLastWriteTime(HostingEnvironment.MapPath(file.IncludedVirtualPath)).Ticks.ToString();
|
|
file.IncludedVirtualPath = string.Concat(file.IncludedVirtualPath, "?v=", lastWrite);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |