Is it possible to detect a bundle querystring in ASP.NET MVC?
For example if I have the following bundle request:
/css/bundles/mybundle.css?v=4Z9jKRKGzlz-D5dJi5VZtpy4QJep62o6A-xNjSBmKwU1
Is it possible to extract the v
querystring?:
4Z9jKRKGzlz-D5dJi5VZtpy4QJep62o6A-xNjSBmKwU1
I've tried doing this in a bundle transform, but with no luck. I found that even with UseServerCache
set to false
the transform code didn't always run.
Generally, the query string is one of client-side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc. In asp.net mvc routing has support for query strings in RouteConfig.
To enable bundling and minification, set the debug value to "false". You can override the Web. config setting with the EnableOptimizations property on the BundleTable class. The following code enables bundling and minification and overrides any setting in the Web.
The only way to safely hide the parameter is to encrypt it.
To improve the performance of the application, ASP.NET MVC provides inbuilt feature to bundle multiple files into a single, file which in turn improves the page load performance because of fewer HTTP requests.
It's been a while since I've worked with the ASP Bundler (I remember it being terrible), and these notes are from my memory. Please verify that it's still valid. Hopefully this will provide a starting point for your search.
To tackle this problem you'll want to explore around in System.Web.Optimization namespace
.
Of most importance is the System.Web.Optimization.BundleResponse
class, which has a method named GetContentHashCode()
which is exactly what you want. Unfortunately, MVC Bundler has a bad architecture and I'm willing to bet that this is still an internal method. This means you won't be able to call it from your code.
Update
Thanks for the verification. So it looks like you have a few ways of accomplishing your goal:
Compute the hash your self using the same algorithm as ASP Bundler
Use reflection to call into the internal method of the Bundler
Get the URL from bundler (there is a public method for this I believe) and extract the query string, then extract the hash from that (using any string extraction methods)
Get angry at Microsoft for bad design
Lets go with #2 (Be careful, since its marked as internal and not part of the public API, a rename of the method by the Bundler team will break things)
//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?###-###...)
var contentHash = method.Invoke(bundleResponse, null);
The bundlePath
variable is the same name that you gave to the bundle (from BundleConfig.cs
)
Hope this helps! Good Luck!
Edit: Forgot to say that it would be a good idea to add a test around this. The test would check for the existence of the GetHashCode
function. This way, in the future, should the internals of the Bundler change the test will fail and you'll know where the problem is.
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