ASP.NET bundling - default virtual paths?
Asked Answered
K

1

6

As far as I can tell (because frankly documentation on System.Web.Optimization is very sparse), the way bundling works in ASP.NET is that you register a set of files (Javascript or CSS, corresponding with ScriptBundle and StyleBundle), and associate them with a virtual path; for example, ~/bundles/jqueryui for all the jQuery UI scripts. Later, ASP.NET can minify these files when you're running in non-debug mode, and the minified version will be made available at the virtual path you specified for the bundle.

The default bundle setup registers some script bundles with ~/bundles/... as the virtual path, and some CSS with ~/Content/css as the virtual path. My question is, isn't this inconsistent? Wouldn't it make more sense to have something like ~/bundles/js/... and ~/bundles/css/...? In fact, the ~/Content directory actually exists as a real directory, so there's even the chance of a name collision. I'd have thought you'd want your virtual bundle paths to be directories that don't exist by default, and are not meant to be created on the actual filesystem. Is there some reason why ~/Content/css is used for the CSS bundle virtual paths that I'm not understanding?

Kitts answered 25/10, 2012 at 9:3 Comment(0)
C
7

Great question, and the short story is that in general it doesn't matter at all. However, some script files and CSS depend on their physical location on disk because they use relative paths to reference other content. For example, of you look at the default jQuery UI bundle in new ASP.NET 4.5 project you see it has a rather long path. That's because jQuery's CSS has relative references to some image files. To get those to resolve correctly the paths matter or else things like ".." will not go up to the right parent directory.

Campanology answered 15/12, 2012 at 17:10 Comment(3)
Thanks, this answer seems to be likely the reason Microsoft did it and I hadn't thought of it. I think it would be more consistent if they had used default paths of something like ~/Content/cssBundles and ~/Content/jsBundles though.Kitts
@jez I work at Microsoft on the MVC team so I know it for sure :)Campanology
There is a CssUrlRewriteTransform now to solve this issue. It will rewrite the URLs in your CSS files, so they're relative to the virtual directory, rather than the original directory; that way, it can find the files in the original directory in spite of the CSS file's new location.Lock

© 2022 - 2024 — McMap. All rights reserved.