Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc bundling and relative css image when website is deployed to an application

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.

like image 883
Ashkan Mobayen Khiabani Avatar asked Sep 06 '15 09:09

Ashkan Mobayen Khiabani


People also ask

What are the benefits that bundling and minification bring in to a web application?

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.

How bundling and minification works in MVC?

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.

What bundle would you use to minify CSS and/or JavaScript on a production environment?

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.

What is the advantage of bundling in MVC?

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.


2 Answers

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);           
    }
}
like image 51
Afshin Mobayen Khiabani Avatar answered Nov 06 '22 19:11

Afshin Mobayen Khiabani


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.

like image 32
Sirwan Afifi Avatar answered Nov 06 '22 19:11

Sirwan Afifi