Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 - Bundling does not work when optimizations are set to true

I wonder what I don't do correct here. I am using ASP.NET C# MVC4 and I want to take use of new css/js optimization feature.

Here is my HTML part

@Styles.Render("~/content/css") 

Here is my BunduleConfig.cs part

bundles.Add(new StyleBundle("~/content/css").Include(                         "~/content/css/reset.css",                         "~/content/css/bla.css"));  // BundleTable.EnableOptimizations = true; 

Output (works):

<link href="/content/css/reset.css" rel="stylesheet"/> <link href="/content/css/bla.css" rel="stylesheet"/> 

However when I uncomment BundleTable.EnableOptimizations = true; html output looks like this

<link href="/content/css?v=5LoJebKvQJIN-fKjKYCg_ccvmBC_LF91jBasIpwtUcY1" rel="stylesheet"/> 

And this is, of course is 404. I have no idea where I did something wrong, please help, first time working with MVC4.

like image 325
Stan Avatar asked Sep 02 '12 22:09

Stan


People also ask

What must be done to enable bundling and minification?

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web. config file. In the following XML, debug is set to true so bundling and minification is disabled. To enable bundling and minification, set the debug value to "false".

What is bundle optimization?

Bundling and minification are the performance optimization techniques that can help to improve load time by reducing the number of requests to the server and reducing the size of requested assets (such as JavaScript and CSS.)

How does bundling work in MVC?

Creating a Bundle in MVCBundling can create separate requests for CSS and JavaScript files. For example, if an application uses both the bootstrap and site CSS for UI design, we can create a common bundle for them. After Bundling, we need to register this bundle in the Application.

What is difference between bundling and minification?

Both bundling and minification are the two separate techniques to reduce the load time. The bundling reduces the number of requests to the Server, while the minification reduces the size of the requested assets.


1 Answers

I imagine the problem is you putting the bundle at a virtual URL that actually exists, but is a directory.

MVC is making a virtual file from your bundle and serving it up from the path you specify as the bundle path.

The correct solution for that problem is to use a bundle path that does not directly map to an existing directory, and instead uses a virtual file name (that also does not map to a real file name) inside that directory.

Example:

If your site has a folder named /content/css, make your css bundle as follows:

In BundleConfig.cs:

bundles.Add(new StyleBundle("~/content/css/AllMyCss.css").Include(                         "~/content/css/reset.css",                         "~/content/css/bla.css")); 

And on the page:

@Styles.Render("~/content/css/AllMyCss.css") 

Note that this assumes you do NOT have a file named AllMyCss.css in your css folder.

like image 116
dodexahedron Avatar answered Sep 20 '22 04:09

dodexahedron