To convert relative image paths to absolute path there are many questions asked and answered in stackoverflow like this one:
MVC4 StyleBundle not resolving images
that suggest adding new CssRewriteUrlTransform()
as below:
bundles.Add(new StyleBundle("~/Content/css/jquery-ui/bundle")
.Include("~/Content/css/jquery-ui/*.css", new CssRewriteUrlTransform()));
and this actually have saved me before. but now that I deploy my website to an application (not root of a website) there is still a problem:
the application is:
http://localhost/sample/
but the image urls are like:
http://localhost/css/imgs/spirit.png
while it should be:
http://localhost/sample/css/imgs/spirit.png
although the bundled css link itself is correct and working.
Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.) Most of the current major browsers limit the number of simultaneous connections per each hostname to six.
Bundling and Minification are two performance improvement techniques that improves the request load time of the application. Most of the current major browsers limit the number of simultaneous connections per hostname to six. It means that at a time, all the additional requests will be queued by the browser.
Click on wwwroot folder, select the css file you want minified, then right click and choose bundler & minifier. Then from popup minify file. It will be the same file name with the minified version.
Bundling and Minification provide us a way to both reduce the number of requests needed to get JS and CSS resource files and reduce the size of the files themselves, thereby improving the responsiveness of our apps. They are nice little optimizations for our MVC apps that can improve performance and add responsiveness.
If there are no virtual directories involved the following code would do:
bundles.Add(new StyleBundle("~/bundles/css").Include(
"~/Content/css/*.css", new CssRewriteUrlTransform()));
but when VirtualDirectory is used CssRewriteUrlTransform will rewrite to Host instead of Host/VirtualDirectory. the solution is to derive CssRewriteUrlTransform which is fully discussed here: ASP.NET MVC4 Bundling with Twitter Bootstrap:
public class CssRewriteUrlTransformWrapper : IItemTransform
{
public string Process(string includedVirtualPath, string input)
{
return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
}
}
This is actually just another issue with optimizations in general not working correctly when the app is running as a virtual directory.
Since you're using a virtual directory for hosting your website, So you should make a wrapper that will call CssRewriteUrlTransform
with the fixed path:
public class CssRewriteUrlTransformWrapper : IItemTransform
{
public string Process(string includedVirtualPath, string input)
{
return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
}
}
More info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With