Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 bundling/minification with IE conditional comments

I am trying to use MVC4's new "bundling and minification".

For IE conditional comments, I'm still doing it the old way: <!--[if lt IE 9]><link href=.../><![endif]--> or <!--[if lt IE 9]>@Styles.Render("~/foo")<![endif]--> but I don't seem to get the automatic debug/release handling.

Is there a built-in way to do this? How are others doing this?

EDIT:
Also it would be great to be able to include <noscript> tags inside the rendered output (used for fallbacks).

like image 502
Bobby B Avatar asked Oct 12 '12 19:10

Bobby B


People also ask

What is difference between bundling and minification?

Bundling and minification are two techniques you can use in ASP.NET to improve page load performance for your web application. Bundling combines multiple files into a single file. Minification performs a variety of different code optimizations to scripts and CSS, which results in smaller payloads.

How to enable bundling and minification in MVC 5?

Bundling and minification can be enabled or disabled in two ways: either setting the value of the debug attribute in the compilation Element in the Web. config file or setting the enableOptimizations property on the BundleTable class. In the following example, debug is set to true in web.

How to create bundling in MVC?

In an ASP.NET MVC Web Application, while creating a project if you select the MVC template then it auto-generated configuration of bundle files. After Project create, You can check under, App_start folder: You'll find BundleConfig file in which all script and style bundles are defined. web.


2 Answers

Until I find a better way, I made an adaptor class called Bundles, which has the method:

public static IHtmlString RenderStylesIe(string ie, params string[] paths) {
  var tag = string.Format("<!--[if {0}]>{1}<![endif]-->", ie, Styles.Render(paths));
  return new MvcHtmlString(tag);
}

There is a similar method for scripts. A view calls them as such:

@Bundles.RenderStylesIe("lt IE 9", "~/Content/foo")
@Bundles.RenderScriptsIe("lte IE 7", "~/Scripts/bar")

If there is a better way, I'd appreciate the advice.

like image 187
Bobby B Avatar answered Nov 10 '22 17:11

Bobby B


The soon to be released 1.1-alpha1 update will have a support doing your own tag formatting with the Scripts/Styles helpers.

There's a new DefaultTagFormat property which is by default set to:

"<script src="{0}"></script>"

There's also a RenderFormat method which takes in the tag format as well. You should be able to control the rendering a bit more with these. Is what you are trying to do possible with in a format string?

like image 40
Hao Kung Avatar answered Nov 10 '22 19:11

Hao Kung