MVC4 StyleBundle Resolution of Images
Bundling CSS in MVC4 poses a challenge when handling standalone CSS and image sets like those found in jQuery UI. To ensure proper resolution of images, follow these guidelines:
Defining the StyleBundle
When creating a StyleBundle, specify a virtual path that does not conflict with a physical content path. For instance:
bundles.Add(new StyleBundle("~/Content/css/jquery-ui") .Include("~/Content/css/jquery-ui/*.css"));
Relative Image Paths
Image paths in the CSS should be relative to the CSS file itself. This is important because the bundle handler will substitute the virtual path in the URL with the actual path to the bundled CSS.
Bundling CSS from the Same Folder
Bundling CSS files from the same folder ensures that relative image paths will continue to work. Define your bundle on the same path as the source files:
bundles.Add(new StyleBundle("~/Content/css/jquery-ui/bundle") .Include("~/Content/css/jquery-ui/*.css"));
CssRewriteUrlTransformation
Alternatively, you can apply a CssRewriteUrlTransformation to rewrite relative URL references to CSS files when bundled. This allows bundling from different folders while maintaining image resolution:
bundles.Add(new StyleBundle("~/Content/css/jquery-ui/bundle") .Include("~/Content/css/jquery-ui/*.css", new CssRewriteUrlTransform()));
The above is the detailed content of How to Resolve Image Paths in MVC4 Style Bundles?. For more information, please follow other related articles on the PHP Chinese website!