MVC4 Bundling: where the URL for bundle is held
Asked Answered
C

3

3

When the bundle is registered in MVC4, what is responsible for "intercepting" incoming http requests for /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

Congou answered 1/10, 2013 at 11:21 Comment(5)
What is the actual problem you're trying to solve?Indoiranian
@Indoiranian I need to 404 requests with old/outdated hashesCongou
Doesn't ASP.NET MVC's bundler do that automatically?Indoiranian
@Indoiranian no, bundle is still served, regardless of whether or not hash is correct (or even present at all)Congou
@Congou if the bundle is served, what is the issue? The hash is there to prevent caching when bundles change. Where would a link with an incorrect bundle hash even be coming from?Albanian
H
1

what is responsible for "intercepting" incoming http requests for ~/bundles/someBundle

There are no incoming requests to ~/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.

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. The actual hash represents a SHA256 hash on this content that is calculated every time you use the Scripts.Render helper.


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.

Hasson answered 1/10, 2013 at 11:44 Comment(3)
thanks, so what is responsible for intercepting request of that "correct url" in the resulting HTML (i've updated my question for more clarity)Congou
Is it possible to interpose BundleModule, to add desired behavior?Congou
No, it's not possible because this module is auto-registered using PreApplicationStart when you reference the System.Web.Optimization assembly to your project.Hasson
C
0

You should have a file called BundleConfig.cs in a folder App_Start in your web project.

That section basically links a url "/bundles/something" to some script(s). 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.

Coercion answered 1/10, 2013 at 12:27 Comment(0)
M
0

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

Mud answered 1/10, 2013 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.