问题描述
MVC是否有内置的方式来为样式表指定CDN后备?我正在尝试为jQuery Mobile Structure样式表设置一个备用.这是我在RegisterBundles
方法中的代码:
var JQMstyleSheet = new StyleBundle("~/JQMstyle", "http://code.jquery.com/mobile/1.3.1/jquery.mobile.structure-1.3.1.min.css").Include("~/theme/jquery.mobile.structure-1.3.1.css");
JQMstyleSheet.CdnFallbackExpression = "window.jQuery.mobile";
bundles.Add(JQMstyleSheet);
页面呈现时,将其输出到html:
<script>
(window.jQuery.mobile)||document.write('<script src="/JQMstyle"><\/script>');
</script>
当CDN失败时,它不会动态添加样式表,就像它对我的javascript文件完全一样.我认为问题在于,当它应该是一种样式时,它正在尝试呈现脚本.除了CdnFallbackExpression
之外,还有其他后备属性吗?
更新
用于System.Web.Optimization.StyleBundle
的Microsoft文档将CdnFallbackExpression
显示为可用属性,但是在说明中显示为获取由Scripts
帮助器类呈现的脚本扩展名...". http://msdn. microsoft.com/en-us/library/system.web.optimization.stylebundle(v=vs.110).aspx 这是System.Web.Optimization.StyleBundle
中的错误吗?不应通过引用Styles
助手类来获取该属性?
TLDR;
查看我的解决方案,该解决方案提供了StyleBundle扩展方法来解决该问题.
也
是的,Microsoft ASP.NET优化框架中有一个错误,记录在此处./p>
解决方案是将CdnFallbackExpression修改为一个javascript函数,该函数既检查样式表又加载回退,从而忽略了 Optimization Framework 中的错误脚本.
有两个棘手的部分,尤其是检查样式表是否来自其他域(例如大多数CDN来源)时是否正在加载.
我在GitHub上有一个解决方案,您可以使用该解决方案,直到框架中解决该问题为止;但是,我仍然要注意确定何时实际加载样式表的棘手部分.
Does MVC have a built in way to specify a CDN fallback for style sheets? I am trying to set up a fallback for the jQuery Mobile Structure style sheet.Here is my code in the RegisterBundles
method:
var JQMstyleSheet = new StyleBundle("~/JQMstyle", "http://code.jquery.com/mobile/1.3.1/jquery.mobile.structure-1.3.1.min.css").Include("~/theme/jquery.mobile.structure-1.3.1.css");
JQMstyleSheet.CdnFallbackExpression = "window.jQuery.mobile";
bundles.Add(JQMstyleSheet);
When the page renders it outputs this to the html:
<script>
(window.jQuery.mobile)||document.write('<script src="/JQMstyle"><\/script>');
</script>
When the CDN fails it doesn't dynamically add the style sheet like it does perfectly for my javascript files.I think the problem is that it is trying to render a script, when it should be a style. Is there a different fallback property other than CdnFallbackExpression
?
UPDATE
The Microsoft docs for System.Web.Optimization.StyleBundle
shows a CdnFallbackExpression
as an available property, however in the description it says "Gets the script extension rendered by the Scripts
helper class..."http://msdn.microsoft.com/en-us/library/system.web.optimization.stylebundle(v=vs.110).aspxIs this a bug in the System.Web.Optimization.StyleBundle
? shouldn't that property by referencing the Styles
helper class?
TLDR;
Check out my solution which provides a StyleBundle extension method to solve the problem.
Also
Yes there is a bug in the Microsoft ASP.NET Optimization Framework, documented here.
The solution is to modify the CdnFallbackExpression to be a javascript function that both checks for the stylesheet and loads the fallback, thus ignoring the bad script from the Optimization Framework.
There are a couple of tricky parts, especially checking for a stylesheet being loaded when it comes from another domain, like most CDN sources.
I have a solution on GitHub that you can use until the issue is fixed in the framework; however, I'd still watch out for the tricky part of determining when the stylesheet is actually loaded.
这篇关于MVC CDN后备样式捆绑包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!