我正在尝试在最近从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
随着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")
在调试模式下,目录中的所有脚本文件将单独呈现;在发布模式下,它们将被捆绑和缩小。