javascript - 在 MVC 4.0 中捆绑 javascript 文件

标签 javascript c# jquery asp.net-mvc asp.net-mvc-4

我正在尝试将 javascript 文件捆绑到我的 Web 应用程序中以提高性能。我试图加载 jQuery 的缩小版本作为示例,但当我运行 Web 应用程序时,jQuery 未加载。

捆绑配置:

  public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").IncludeDirectory("~/Scripts/","jquery-1.8.2.min.js"));
//Also tried this:
//bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-1.8.2.min.js"));
            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
        }
    }

母版页:

@using System.Configuration
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")   
    <script type="text/javascript">
        var screenRefreshTime = '@ConfigurationManager.AppSettings["ScreenRefreshTime"].ToString()';
        screenRefreshTime = parseInt(screenRefreshTime);
    </script>
</head>
<body>
    @RenderBody()
    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>

最佳答案

在为其创建 bundle 时,无需包含 min。我假设您在同一文件夹中有 jquery.1.8.2.js 和 jquery.1.8.2.min.js,并且文件名相同,除了“min” “单词例如:jquery.1.8.2.js,jquery.1.8.2.min.js。您可以尝试直接包含工作正常,而不是包含目录。代码是这样的

  public class BundleConfig
  {
    public static void RegisterBundles(BundleCollection bundles)
    {

        bundles.Add(new ScriptBundle("~/bundles/jquery").Include("jquery-1.8.2.js"));

    }
}

还需要提及的是,捆绑仅适用于 Release模式,如果您需要在 Debug模式下检查它,则需要启用优化。

BundleTable.EnableOptimizations = true;

调用它

 @Scripts.Render("~/bundles/jquery")

关于javascript - 在 MVC 4.0 中捆绑 javascript 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26864339/

相关文章:

javascript - 使侧导航高度匹配 Angular ui-view div 高度

javascript - 在 Javascript 函数中返回值

c# - 焦点不应在 C# 中的箭头键上移动

jquery - 使用 jQuery 和 ASP.NET 用户控件时如何处理元素 ID

javascript - riot js this.update 奇怪的行为

JavaScript 强制视频在移动设备上自动播放

c# - 重定向到 Razor 宏错误的自定义错误页面

c# - 在 .NET 4.0 中通过服务引用使用 WCF 服务

jquery - 灯箱画廊中的超链接

javascript - 为什么我的表单不删除多个条目?