我正在尝试将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>
答案 0 :(得分:1)
IncludeDirectory的第二个参数是搜索模式,因此请尝试* .js
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").IncludeDirectory("~/Scripts/", "*.js"));
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
}
}
或使用Include而不是IncludeDirectory:
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-1.8.2.min.js"));
bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));
}
}
答案 1 :(得分:1)
当你为它创建包时,你不需要包含min。我假设你在相同的文件夹中有jquery.1.8.2.js和jquery.1.8.2.min.js,文件名是相同的,除了&# 34;分钟&#34;单词Ex: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"));
}
}
如果需要在调试模式下进行检查,还需要启用优化,还需要提及仅在发布模式下捆绑工作。
BundleTable.EnableOptimizations = true;
要打电话
@Scripts.Render("~/bundles/jquery")