c# - 将 Bundle 添加到现有的 ASP.NET Webforms 解决方案

标签 c# asp.net webforms bundle

我正在尝试将 bundle 添加到现有的 ASP.NET Webforms 解决方案,但我的 bundle 始终呈现为空,我不确定为什么。我一直在关注this blog post .

到目前为止我有:

  • 添加了 Microsoft ASP.NET Web 优化框架 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

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

Site.Master

<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 开发工具检查资源时,我可以看到

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

但是当检查它们时,它们没有内容。

最佳答案

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

在 Site.Master 中,我错过了 bundle 末尾的“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>

关于c# - 将 Bundle 添加到现有的 ASP.NET Webforms 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508805/

相关文章:

c# - 如何在 UWP 应用程序中创建 COM 对象? (C#)

c# - smtp.live.com - 邮箱不可用。服务器响应为 : 5. 7.3 请求的操作已中止;用户未通过身份验证

c# - 具有自定义端点名称的 WEB API .NET 操作

ASP.NET 空网站模板

c# - 需要定义控制流和功能

c# - Blazor 服务器端 : How can I read out the local time of the user/browser not of the server (using only c#)

asp.net - 有没有比 FindControl() 更好的方法来访问 ITemplate 中的控件?

javascript 将 body_0$ 添加到 ID 选择器

c# - PageMethod 未定义错误

asp.net - 使用 ITextSharp 流式传输分割的 pdf 以将其存储在 Azure 中