My question is similar to this:
Except that I want to stick with MVC's own bundling if I can. I'm having a brain crash trying to figure out what the correct pattern is for specifying style bundles such that standalone css and image sets such as jQuery UI work.
I have a typical MVC site structure with /Content/css/
which contains my base CSS such as styles.css
. Within that css folder I also have subfolders such as /jquery-ui
which contains its CSS file plus an /images
folder. Image paths in the jQuery UI CSS are relative to that folder and I don't want to mess with them.
As I understand it, when I specify a StyleBundle
I need to specify a virtual path which does not also match a real content path, because (assuming I'm ignoring routes to Content) IIS would then try to resolve that path as a physical file. So I'm specifying:
bundles.Add(new StyleBundle("~/Content/styles/jquery-ui")
.Include("~/Content/css/jquery-ui/*.css"));
rendered using:
@Styles.Render("~/Content/styles/jquery-ui")
I can see the request going out to:
http://localhost/MySite/Content/styles/jquery-ui?v=nL_6HPFtzoqrts9nwrtjq0VQFYnhMjY5EopXsK8cxmg1
This is returning the correct, minified CSS response. But then the browser sends a request for a relatively linked image as:
http://localhost/MySite/Content/styles/images/ui-bg_highlight-soft_100_eeeeee_1x100.png
Which is a 404
.
I understand that the last part of my URL jquery-ui
is an extensionless URL, a handler for my bundle, so I can see why the relative request for the image is simply /styles/images/
.
So my question is what is the correct way of handling this situation?
As of v1.1.0-alpha1 (pre release package) the framework uses the
VirtualPathProvider
to access files rather than touching the physical file system.The updated transformer can be seen below:
Here is a Bundle Transform that will replace css urls with urls relative to that css file. Just add it to your bundle and it should fix the issue.
Although Chris Baxter's answer helps with original problem, it doesn't work in my case when application is hosted in virtual directory. After investigating the options, I finished with DIY solution.
ProperStyleBundle
class includes code borrowed from originalCssRewriteUrlTransform
to properly transform relative paths within virtual directory. It also throws if file doesn't exist and prevents reordering of files in the bundle (code taken fromBetterStyleBundle
).Use it like
StyleBundle
:Grinn / ThePirat solution works well.
I did not like that it new'd the Include method on bundle, and that it created temporary files in the content directory. (they ended up getting checked in, deployed, then the service wouldn't start!)
So to follow the design of Bundling, I elected to perform essentially the same code, but in an IBundleTransform implementation::
And then wrapped this up in a Bundle Implemetation:
Sample Usage:
Here is my extension method for RelativeFromAbsolutePath:
CssRewriteUrlTransform
fixed my problem.If your code still not loading images after using
CssRewriteUrlTransform
, then change your css filename's from:To:
Someway .(dots) are not recognizing in url.
You can simply add another level of depth to your virtual bundle path
This is a super low-tech answer and kind of a hack but it works and won't require any pre-processing. Given the length and complexity of some of these answers I prefer doing it this way.