Search code examples
asp.net-mvcasp.net-mvc-4conditional-commentsasp.net-optimizationbundling-and-minification

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


Solution

  • 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.