【问题标题】:Should MVC Bundling allow multiple CDN paths to be included for each file in the bundle?MVC 捆绑是否应该允许捆绑中的每个文件包含多个 CDN 路径?
【发布时间】:2014-09-25 14:32:08
【问题描述】:

我想捆绑 2 个与 Twitter Bootstraps 相关的 css 文件。

"~/Content/Themes/Base/CSS/bootstrap-theme.css"
"~/Content/Themes/Base/CSS/bootstrap.css"

每个文件都有一个指向其 CDN 的单独 URL:

//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css
//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css

如何在同一个 StyleBundle 中为 2 个单独的 css 文件提供 2 个 CDN 路径? 我是否需要分别创建 2 个 StyleBundle,每个都包含一个 css 文件?这似乎对捆绑的努力适得其反。

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: asp.net-mvc bundling-and-minification


    【解决方案1】:

    您将捆绑如下:

    bundles.Add(new StyleBundle("~/Content/bootstrapTheme", "http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css").Include(
                    "~/Content/Themes/Base/CSS/bootstrap-theme.css"));
    
    bundles.Add(new StyleBundle("~/Content/bootstrap", "http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css").Include(
                    "~/Content/Themes/Base/CSS/bootstrap.css"));
    

    这不会适得其反,因为您需要了解为什么 MVC 允许您捆绑 CDN 路径: 与CDN捆绑时,生产时引用CDN标签,调试时引用本地版本。

    在您的 webconfig 中,您可以设置渲染哪个: 设置<compilation debug="true"/> 使用本地文件,设置<compilation debug="false"/> 使用您的CDN。

    要在你的视图中调用它:(注意:你在 () 之间放置的内容必须与上面 StyleBundle 中的第一个参数相同。

    @Styles.Render("~/Content/bootstrapTheme")
    @Styles.Render("~/Content/bootstrap")
    

    【讨论】:

    • 如果捆绑意味着将多个文件组合在一起,则有条件地切换标签不符合捆绑的条件。但我想,这种方式只是更方便,而不是简单地为 CDN 条件切换引入另一个东西。
    • @Believe2014 我明白你的意思。我想这正是微软决定这样做的方式 =( 你可以让你自己的库做你想做的事。祝你好运!
    【解决方案2】:

    如下图添加:

    url("http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600");
    

    【讨论】:

    • 您的回答未涉及 MVC 捆绑功能。它也与上下文无关。这个问题的未来研究人员将根本无法使用 url() 函数,而不知道它是 C# 还是 Javascript,以及它如何适应编译和捆绑静态文件的上下文。
    猜你喜欢
    • 1970-01-01
    • 2012-10-28
    • 2015-09-12
    • 2015-08-02
    • 2015-01-25
    • 1970-01-01
    • 2023-03-07
    • 2020-05-20
    • 2014-12-23
    相关资源
    最近更新 更多