【问题标题】:Include js in Bundle.config in asp.net application在 asp.net 应用程序的 Bundle.config 中包含 js
【发布时间】:2017-10-17 01:56:58
【问题描述】:

我正在尝试将我的 css 和 js 捆绑到我的 asp.net Web 应用程序中。 CSS 工作正常,但它也将 js 文件作为 css。可能是我做了一些错误配置。当我调试我的应用程序并查看页面源代码时,它会显示如下所示。

<link href="/Content/bootstrap.css" rel="stylesheet"/>
<link href="/Content/Site.css" rel="stylesheet"/>
<link href="/Content/owlcarousel.css" rel="stylesheet"/>
<link href="/Scripts/jquery.js" rel="stylesheet"/>
<link href="/Scripts/owlcarousel.js" rel="stylesheet"/>

Bundle.config

<?xml version="1.0" encoding="utf-8" ?>
<bundles version="1.0">
  <styleBundle path="~/Content/css">
    <include path="~/Content/bootstrap.css" />
    <include path="~/Content/Site.css" />
    <include path="~/Content/owlcarousel.css" />
  </styleBundle>

  <scriptBundle path="~/Scripts/js">
    <include path="~/Scripts/jquery.js" />
    <include path="~/Scripts/owlcarousel.js" />
  </scriptBundle>
</bundles>

母版页

<asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>
    <webopt:bundlereference runat="server" path="~/Content/css" />
    <webopt:bundlereference runat="server" path="~/Scripts/js" />

【问题讨论】:

  • @H.Pauwelyn ~/Content/css 是仅包含 css 文件的目录。 ~/Scripts/js 用于 JS

标签: javascript css asp.net bundle


【解决方案1】:

虽然我不知道解决方案的确切原因,但我可以大胆猜测。

看来您需要使用 &lt;asp:PlaceHolder runat="server"&gt;&lt;%: Scripts.Render("~/Scripts/js") %&gt;&lt;/asp:PlaceHolder&gt; 来获取 Bundle.config scriptBundle 引用才能正确呈现。

我在搜索过程中看到一篇文章(遗憾的是现在丢失了),指出“webopt:BundleReference”调用了 Styles.Render...这是我怀疑你和我所经历的,即 Microsoft 将 Styles.Render 应用于scriptBundle 引用而不是 Scripts.Render!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 2012-01-16
    相关资源
    最近更新 更多