MVC4 bundling/minification with IE conditional comments

僤鯓⒐⒋嵵緔 提交于 2019-11-29 02:41:16

问题


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


回答1:


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.




回答2:


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?




回答3:


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

)



来源:https://stackoverflow.com/questions/12865939/mvc4-bundling-minification-with-ie-conditional-comments

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