在这种情况下,捆绑会为您提供帮助。您应该已经能够在 App_Start 的 BundleConfig.cs 文件中看到一个示例。
这是一个关于如何配置它的教程http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
从该教程中,这就是您实际配置包本身、将在其中包含的文件等的方式。这包括脚本文件夹中的任何 jquery 到一个名为 ~/bundles/jquery 的包中。
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
// Code removed for clarity.
BundleTable.EnableOptimizations = true; // <-- this line overrides the debug check to show you the minified version even in debug mode... remove it for normal .min in debug/un-minned when not in debug behaviour
}
然后您使用
从您的视图中引用这些
@Scripts.Render("~/bundles/jquery")
然后,如果您未处于调试状态,这将使用 jquery 的 .min 版本呈现脚本标记,如果您处于调试状态,则将其排除在外。在同一个脚本文件夹中需要有一个 .min 版本和一个未缩小的版本(如果 Web Essentials 正在为您创建 .min,您应该拥有它。
如果您使用 ScriptBundles,您实际上可以停止使用 Web Essentials 进行缩小,因为它们会在将 javascript 打包到包中时为您缩小。
供您更新
"~/Scripts/jquery-{version}.js" 表示匹配脚本文件夹中以jquery- 开头并以.js 结尾并且之间有一些版本号的任何文件。如果文件中没有版本号,请不要尝试使用 {version} 替换。
在这种情况下,他们使用 jquery 执行此操作,因此如果您升级您正在使用的 jquery 版本,您不必返回到您的 BundleConfig 并手动更改 jquery 引用的文件名。
如果您的文件名为 site1.3.7.css,那么这可能会起作用。
bundles.Add(new StyleBundle("~/Content/css")
.Include("~/Content/site{version}.css"));
但听起来您更有可能只需要site.css。
bundles.Add(new StyleBundle("~/Content/css")
.Include("~/Content/site.css"));
我不确定您认为是什么阻止了您使用它们,但您可以链接到 CDN 中的文件并缩小或不缩小。甚至只是将单个文件打包在一起,以便在调试之外获得缩小的好处,而无需“捆绑”它们。所以可能有办法。