获取MVC捆绑查询字符串 [英] Get MVC Bundle Querystring

查看:189
本文介绍了获取MVC捆绑查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能检测ASP.NET MVC捆绑查询字符串?

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

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

是否可以提取 v 查询字符串:

Is it possible to extract the v querystring?:

4Z9jKRKGzlz-D5dJi5VZtpy4QJep62o6A-xNjSBmKwU1

4Z9jKRKGzlz-D5dJi5VZtpy4QJep62o6A-xNjSBmKwU1

我试过在一个包变换这样做,但没有运气。我发现,即使与 UseServerCache 设置为变换code并不总是运行。


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.

推荐答案

它已经有一段时间,因为我已经与ASP捆扎机(我记得它是狗屎总)工作过,这些钞票是从我的记忆。请验证其仍然有效。
当我回到我的开发计算机上我会更新这个。希望这会为您的搜索提供了一个起点。

Its been a while since I've worked with the ASP Bundler (I remember it being total shit), and these notes are from my memory. Please verify its still valid. I will update this when I get back on my development computer. Hopefully this will provide a starting point for your search.

要解决这个问题,你会想在System.Web.Optimization空间探索周围。

To tackle this problem you'll want to explore around in System.Web.Optimization namespace.

最重要的是System.Web.Optimization.BundleResponse类,它有C $ C()命名GetContentHash $的方法这正是你想要的。不幸的是,MVC捆扎机有一个蹩脚的架构,我敢打赌,这仍是一个内部方法。这意味着你将无法从您的code调用它。

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 crappy 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.

我会尝试当我回家(我开发框),以进一步探讨这个命名空间

I'll try to explore this namespace further when I get home (on my dev box)

-----更新----

-----Update----

感谢您的验证。所以它看起来像你必须完成你的目标的几种方式:

Thanks for the verification. So it looks like you have a few ways of accomplishing your goal:

1),使用相同的算法的ASP捆扎机计算散列你的自我

1) Compute the hash your self using the same algorithm as ASP Bundler

2)使用反射来调用到捆扎机的内部方法

2) Use reflection to call into the internal method of the Bundler

3)获取从打捆的URL(有这种我相信一个公共方法),并提取出查询字符串,然后提取从哈希(使用任何字符串extration方法)

3) Get the url from bundler (there is a public method for this I believe) and extract out the query string, then extract the hash from that (using any string extration methods)

4)在微软上火蹩脚的设计

4) Get angry at Microsoft for crappy design

让我们一起去#2(要小心,因为它标记为公共API由捆扎机团队方法的命名会打破你的内部,而不是一部分)

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 you)

//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);

该bundlePath变量是你给包(从BundleConfig.cs)

The bundlePath variable is the same name that you gave to the bundle (from BundleConfig.cs)

希望这有助于!祝你好运!

Hope this helps! Good Luck!

编辑:忘了说,这将是添加解决这个测试是一个好主意。该测试将检查GetHash code函数是否存在等。这样一来,今后,应在捆扎机的内部变化会导致测试失败,你就会知道问题出在哪里。

Forgot to say that it would be a good idea to add a test around this. The test would check for the existance 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.

这篇关于获取MVC捆绑查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆