向现有的ASP.NET Webforms解决方案添加捆绑包

44

我正在尝试向现有的ASP.NET Webforms解决方案中添加Bundles,但我的Bundles始终为空,并且我不确定原因。我一直在遵循这篇博客文章

到目前为止,我已经:

  • 添加了Microsoft ASP.NET Web Optimization Framework NuGet包
  • 确保包含必需的引用
  • 尝试在Web.config中使用debug="false"和debug="true"
  • 向我的解决方案中添加了以下代码

Global.asax.cs

protected void Application_Start(object sender, EventArgs e)
{
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

App_Start/BundleConfig.cs

-->

App_Start/BundleConfig.cs

public class BundleConfig
{
    // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkID=303951
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/Global").Include(
            "~/js/jquery-{version}.js",
            "~/js/jquery-ui.js"));

        bundles.Add(new ScriptBundle("~/bundles/GlobalHead").Include(
            "~/js/modernizr*"));

        bundles.Add(new StyleBundle("~/Content/Global").Include(
            "~/css/site.css"));
    }
}

站点主控制页

<head runat="server">
    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundle/GlobalHead") %>
        <%: Styles.Render("~/Content/Global") %>
    </asp:PlaceHolder>
</head>
<body>
    <%: Scripts.Render("~/bundle/Global") %>
</body>

Web.Config

<namespaces>
    <add namespace="System.Web.Optimization" />
</namespaces>

更新

明确一下,当我打开一个网页并使用Chrome DevTools检查资源时,我可以看到:

Content/Site.css
bundle/Global.js
bundle/GlobalHead.js

但是在检查它们时,它们没有任何内容。

1个回答

31

简单的解决方案,我有一些打字错误。

在Site.Master中,我错过了bundles结尾的's'。使我的Site.Master看起来像这样。

<head runat="server">
    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/GlobalHead") %>
        <%: Styles.Render("~/Content/Global") %>
    </asp:PlaceHolder>
</head>
<body>
    <%: Scripts.Render("~/bundles/Global") %>
</body>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接