MVC4 bundling/minification with IE conditional comments

时光总嘲笑我的痴心妄想 提交于 2019-11-30 05:00:55

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?

An alternative I found was here: https://coderwall.com/p/5zqvkg

It is a matter of preference

(Still love code above - it is much cleaner and reads easier

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

)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!