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).
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.
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.
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.
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.
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?
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