我正在尝试在最近从MVC 3转换到MVC 4 beta版的项目中使用新的捆绑功能。它需要一行全局代码。asax, BundleTable.Bundles.RegisterTemplateBundles();),需要使用System.Web.Optimization;在顶端。

当我这样做时,我得到红色的弯曲线条,表示“您是否缺少一个汇编引用?”当我尝试添加引用,并单击对话框中的. net选项卡,从A-Z排序,我没有看到System.Web.Optimization。

如何将此引用添加到我的项目中?


当前回答

更新 1.1版。x可用,请阅读发布说明:https://www.nuget.org/packages/Microsoft.AspNet.Web.Optimization


优化包现在已经过时了。ASP。NET (MVC) 4及以上版本,您应该安装Microsoft ASP。NET Web优化框架:

Install the package from nuget: Install-Package Microsoft.AspNet.Web.Optimization Create and configure bundle(s) in App_Start\BundleConfig.cs: public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/Scripts/jquery").Include( "~/Scripts/Lib/jquery/jquery-{version}.js", "~/Scripts/Lib/jquery/jquery.*", "~/Scripts/Lib/jquery/jquery-ui-{version}.js") ); bundles.Add(new ScriptBundle("~/Scripts/knockout").Include( "~/Scripts/Lib/knockout/knockout-{version}.js", "~/Scripts/Lib/knockout/knockout-deferred-updates.js") ); } } Call the RegisterBundles() function from Application_Start() in your global.asax.cs: using System.Web.Optimization; protected void Application_Start() { ... BundleConfig.RegisterBundles(BundleTable.Bundles); ... } In your view.cshtml include the Optimization namespace and render the bundle(s): @using System.Web.Optimization @Scripts.Render("~/Scripts/jquery") @Scripts.Render("~/Scripts/knockout")

更多信息请参见http://www.asp.net/mvc/overview/performance/bundling-and-minification

其他回答

设置为全局。asax application_start(在RELEASE模式等):

BundleTable.EnableOptimizations = **true**;

在DEBUG模式下启用缩小并更改为false以单独呈现所有脚本和样式文件。

从NUGet通过Visual Studio安装它 打开Visual Studio 2010,选择工具->库包管理器->包管理器控制台

这将打开conslve,粘贴

Install-Package Microsoft.AspNet.Web.Optimization 

并输入。这样就做完了

在我的情况下,这是一个tfs的问题,因为tfs排除二进制文件,所以Nugget PM找到安装的金块,不更新库 如果你有类似的问题:

转到源代码控制 导航到..\packages\Microsoft.Web.Optimization 添加lib文件夹(取消排除二进制扩展) 更新解决方案并从路径中添加dll引用

注意:包文件夹是在同一级别的解决方案。sln文件

更新 1.1版。x可用,请阅读发布说明:https://www.nuget.org/packages/Microsoft.AspNet.Web.Optimization


优化包现在已经过时了。ASP。NET (MVC) 4及以上版本,您应该安装Microsoft ASP。NET Web优化框架:

Install the package from nuget: Install-Package Microsoft.AspNet.Web.Optimization Create and configure bundle(s) in App_Start\BundleConfig.cs: public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/Scripts/jquery").Include( "~/Scripts/Lib/jquery/jquery-{version}.js", "~/Scripts/Lib/jquery/jquery.*", "~/Scripts/Lib/jquery/jquery-ui-{version}.js") ); bundles.Add(new ScriptBundle("~/Scripts/knockout").Include( "~/Scripts/Lib/knockout/knockout-{version}.js", "~/Scripts/Lib/knockout/knockout-deferred-updates.js") ); } } Call the RegisterBundles() function from Application_Start() in your global.asax.cs: using System.Web.Optimization; protected void Application_Start() { ... BundleConfig.RegisterBundles(BundleTable.Bundles); ... } In your view.cshtml include the Optimization namespace and render the bundle(s): @using System.Web.Optimization @Scripts.Render("~/Scripts/jquery") @Scripts.Render("~/Scripts/knockout")

更多信息请参见http://www.asp.net/mvc/overview/performance/bundling-and-minification

随着ASP的最终发布版本。Net MVC 4的方法如下:

Install Microsoft.AspNet.Web.Optimization via nuget (as it is not installed by the framework) install-package Microsoft.AspNet.Web.Optimization Create the bundle in Global.asax Application_Start: var scripts = new ScriptBundle("~/MyBundle"); scripts.IncludeDirectory("~/Scripts/MyDirectory", "*.js"); BundleTable.Bundles.Add(scripts); Add the "System.Web.Optimization" namespace to the "Views" web.config: <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Optimization" /> </namespaces> </pages> In your view.cshtml add an include to the bundle created in the last step: @Scripts.Render("~/MyBundle")

在调试模式下,目录中的所有脚本文件将单独呈现;在发布模式下,它们将被捆绑和缩小。