It has been a long time since I worked with the ASP Bundler (I remember that it is terrible), and these notes have remained in my memory. Please make sure it is still valid. Hope this serves as a starting point for your search.
To solve this problem, you will want to examine it in System.Web.Optimization namespace .
The most important is the System.Web.Optimization.BundleResponse class, which has a method called GetContentHashCode() , exactly what you need. Unfortunately, the MVC Bundler has a poor architecture, and I bet it's still an internal method. This means that you cannot call it from your code.
Update
Thanks for checking out. It looks like you have several ways to achieve your goal:
Calculate the hash yourself using the same algorithm as the ASP Bundler
Use reflection to call the Bundler internal method
Get the URL from the package (for this, I suppose, there is a public method) and extract the query string and then extract the hash from it (using any string extraction methods)
Get mad at Microsoft for bad design
Let's move on to # 2 (be careful, because it is marked as internal and is not part of the public API, renaming the method with the Bundler command will break)
//This is the url passed to bundle definition in BundleConfig.cs string bundlePath = "~/bundles/jquery"; //Need the context to generate response var bundleContext = new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, bundlePath); //Bundle class has the method we need to get a BundleResponse Bundle bundle = BundleTable.Bundles.GetBundleFor(bundlePath); var bundleResponse = bundle.GenerateBundleResponse(bundleContext); //BundleResponse has the method we need to call, but its marked as //internal and therefor is not available for public consumption. //To bypass this, reflect on it and manually invoke the method var bundleReflection = bundleResponse.GetType(); var method = bundleReflection.GetMethod("GetContentHashCode", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); //contentHash is whats appended to your url (url?
The bundlePath variable is the same name you gave the package (from BundleConfig.cs )
Hope this helps! Good luck
Change: Forgot to say that it would be nice to add a test around this. The test will check for the GetHashCode function. Thus, in the future, if the internal Bundler device changes the test, it will fail and you will find out what the problem is.
Frison Alexander
source share