Get Querystring MVC Bundle

Is it possible to detect a query chain in ASP.NET MVC?

For example, if I have the following package request:

/css/bundles/mybundle.css?v=4Z9jKRKGzlz-D5dJi5VZtpy4QJep62o6A-xNjSBmKwU1

Is it possible to extract query v ?

:

4Z9jKRKGzlz-D5dJi5VZtpy4QJep62o6A-xNjSBmKwU1


I tried to do this in a package conversion, but no luck. I found that even with UseServerCache set to false , the conversion code did not always run.

+7
c # query-string asp.net-mvc bundling-and-minification
source share
1 answer

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:

  1. Calculate the hash yourself using the same algorithm as the ASP Bundler

  2. Use reflection to call the Bundler internal method

  3. 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)

  4. 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?###-###...) var contentHash = method.Invoke(bundleResponse, null); 

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.

+4
source share

All Articles