I've been looking for any examples or references where ASP.NET Optimization Framework is used with UseCDN = true and HashContent Number is attached to the Bundles' URI. Unfortunately without any luck. The following is a simplified example of my code.
My Bundling code is pretty Simple
bundles.UseCdn = true;
BundleTable.EnableOptimizations = true;
var stylesCdnPath = "http://myCDN.com/style.css";
bundles.Add(new StyleBundle("~/bundles/styles/style.css", stylesCdnPath).Include(
"~/css/style.css"));
I call the Render from a Master page
<%: System.Web.Optimization.Styles.Render("~/bundles/styles/style.css")%>
The generated code is
<link href="http://myCDN.com/style.css" rel="stylesheet"/>
If I disable UseCDN
/bundles/styles/style.css?v=geCEcmf_QJDXOCkNczldjY2sxsEkzeVfPt_cGlSh4dg1
How can I make the bunlding add v= Hash Content when useCDN is set to true ?
Edit:
I tried using
<%: System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/bundles/styles/style.css",true)%>
it still will not generate v = hash if CdnUse = true
You can't, Setting UseCdn
to true
means ASP.NET will serve bundles as is from your CDN Path, no Bundling and Minification will be performed.
The query string v has a value token that is a unique identifier used for caching. As long as the bundle doesn't change, the ASP.NET application will request the bundle using this token. If any file in the bundle changes, the ASP.NET optimization framework will generate a new token, guaranteeing that browser requests for the bundle will get the latest bundle.
Have a look at BundleCollection.ResolveBundleUrl
Implementation:
// System.Web.Optimization.BundleCollection
/// <summary>Returns the bundle URL for the specified virtual path, including a content hash if requested.</summary>
/// <returns>The bundle URL or null if the bundle cannot be found.</returns>
/// <param name="bundleVirtualPath">The virtual path of the bundle.</param>
/// <param name="includeContentHash">true to include a hash code for the content; otherwise, false. The default is true.</param>
public string ResolveBundleUrl(string bundleVirtualPath, bool includeContentHash)
{
Exception ex = ExceptionUtil.ValidateVirtualPath(bundleVirtualPath, "bundleVirtualPath");
if (ex != null)
{
throw ex;
}
Bundle bundleFor = this.GetBundleFor(bundleVirtualPath);
if (bundleFor == null)
{
return null;
}
if (this.UseCdn && !string.IsNullOrEmpty(bundleFor.CdnPath))
{
return bundleFor.CdnPath;
}
return bundleFor.GetBundleUrl(new BundleContext(this.Context, this, bundleVirtualPath), includeContentHash);
}
Ref: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
Update
You can have the V hash manually added to your CDN by implementing your own bundle and generate the hash on ApplyTransforms
call:
public class myStyleBundle: StyleBundle
{
public myStyleBundle(string virtualPath)
:base(virtualPath)
{
}
public myStyleBundle(string virtualPath, string cdnPath)
: base(virtualPath,cdnPath)
{
MyCdnPath = cdnPath;
}
public string MyCdnPath
{
get;
set;
}
public override BundleResponse ApplyTransforms(BundleContext context, string bundleContent, System.Collections.Generic.IEnumerable<BundleFile> bundleFiles)
{
var response = base.ApplyTransforms(context, bundleContent, bundleFiles);
base.CdnPath = string.Format("{0}?v={1}", this.MyCdnPath, this.HashContent(response));
return response;
}
private string HashContent(BundleResponse response)
{
string result;
using (SHA256 sHA = new SHA256Managed())
{
byte[] input2 = sHA.ComputeHash(Encoding.Unicode.GetBytes(response.Content));
result = HttpServerUtility.UrlTokenEncode(input2);
}
return result;
}
}
Then, simply do:
bundles.Add(new myStyleBundle("~/bundles/styles/style.css", stylesCdnPath).Include(
"~/css/style.css"));
Please note that System.Web.Optimization.BundleResponse
creates a hash algorithm based on environment settings:
// System.Web.Optimization.BundleResponse
private static SHA256 CreateHashAlgorithm()
{
if (BundleResponse.AllowOnlyFipsAlgorithms)
{
return new SHA256CryptoServiceProvider();
}
return new SHA256Managed();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With