gpt4 book ai didi

javascript - 在 MVC 4.0 中捆绑 javascript 文件

转载 作者:行者123 更新时间:2023-12-02 16:48:52 25 4
gpt4 key购买 nike

我正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com