简体   繁体   English

MVC4 Bundling:包含bundle的URL的位置

[英]MVC4 Bundling: where the URL for bundle is held

When the bundle is registered in MVC4, what is responsible for "intercepting" incoming http requests for /bundles/someBundle?v=1hDzBpmYJm4Iu-OjRN1YqS1WeNThVl0kStLJGP8WCr41 ? 当捆绑包在MVC4中注册时,负责“拦截” /bundles/someBundle?v=1hDzBpmYJm4Iu-OjRN1YqS1WeNThVl0kStLJGP8WCr41传入http请求是/bundles/someBundle?v=1hDzBpmYJm4Iu-OjRN1YqS1WeNThVl0kStLJGP8WCr41 also since hash for each bundle is calculated only once (at a first request), where is that actually held, - and is it possible to return 404 if an incoming hash does not match 因为每个包的哈希只计算一次(在第一次请求时),实际持有的地方, - 如果传入的哈希不匹配,是否可以返回404

what is responsible for "intercepting" incoming http requests for ~/bundles/someBundle 什么负责“拦截”〜/ bundles / someBundle的传入http请求

There are no incoming requests to ~/bundles/someBundle . 没有来自~/bundles/someBundle传入请求。 It's the server side helper that you are using ( Scripts.Render ) that on the server (within the same HTTP requests) interprets this value and spits the correct url in the resulting HTML. 它是您正在使用的服务器端帮助程序( Scripts.Render ),它在服务器上(在相同的HTTP请求中)解释此值并在生成的HTML中吐出正确的URL。

also since hash for each bundle is calculated only once (at a first request), where is that actually held, 因为每个包的哈希只计算一次(在第一次请求时),实际持有的位置,

The actual bundle contents is stored in the server side cache : HttpContext.Cache . 实际的bundle内容存储在服务器端缓存中: HttpContext.Cache The actual hash represents a SHA256 hash on this content that is calculated every time you use the Scripts.Render helper. 实际哈希表示此内容上的SHA256哈希值,每次使用Scripts.Render帮助程序时都会计算该哈希值。


UPDATE: 更新:

It's the System.Web.Optimization.BundleModule that is auto-registered when you reference the System.Web.Optimization assembly that is responsible for intercepting requests to urls like /bundles/someBundle?v=1hDzBpmYJm4Iu-OjRN1YqS1WeNThVl0kStLJGP8WCr41 and returning the actual contents. 当您引用System.Web.Optimization程序集时,会自动注册System.Web.Optimization.BundleModule ,该程序集负责拦截对诸如/bundles/someBundle?v=1hDzBpmYJm4Iu-OjRN1YqS1WeNThVl0kStLJGP8WCr41类的URL的请求/bundles/someBundle?v=1hDzBpmYJm4Iu-OjRN1YqS1WeNThVl0kStLJGP8WCr41并返回实际内容。

You should have a file called BundleConfig.cs in a folder App_Start in your web project. 您应该在Web项目的App_Start文件夹中有一个名为BundleConfig.cs的文件。

That section basically links a url "/bundles/something" to some script(s). 该部分基本上将URL“/ bundles / something”链接到某些脚本。 When in accessing the site in Release mode (not debug activated), it will automatically merge the script into one in-memory file, minimize the script, add caching headers to the request and generate a hash of the file content. 在以发布模式访问站点(未激活调试)时,它会自动将脚本合并到一个内存中文件中,最小化脚本,向请求添加缓存头并生成文件内容的哈希。

If you are in debug, all the scripts should be separated to make debugging easier. 如果您在调试中,则应分离所有脚本以使调试更容易。

You either redefine the bundles you see in that file or declare some of your own. 您要么重新定义在该文件中看到的包,要么声明自己的包。

Enjoy. 请享用。

The reason to append a query string with a parameter based on contents of actual files that you are serving is solving the caching problem. 使用基于您正在服务的实际文件的内容的参数附加查询字符串的原因是解决缓存问题。 Sou you can inform browsers to cache this requests for a long period of time and speed up subsequent pages load times. 您可以通知浏览器长时间缓存此请求并加快后续页面加载时间。
So for developers of this bundling mechanism there is no difference what that parameter is. 因此对于这种捆绑机制的开发人员而言,该参数没有区别。 Only thing that is important is that if you change contents of your scripts or css - hash would change and it will force clients browser to request new files from the server. 唯一重要的是,如果您更改脚本或css的内容 - 哈希将更改,它将强制客户端浏览器从服务器请求新文件。
As for what is responsible for intersepting this requests - there is source code of MVC available on codeplex, but i guess it plugs right into routing. 至于什么负责插入这个请求 - 在Codeplex上有MVC的源代码,但我猜它插入到路由中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM