本文介绍了如何为 MVC-3-converted-to-4 应用程序添加对 System.Web.Optimization 的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use the new bundling feature in a project I recently converted from MVC 3 to MVC 4 beta. It requires a line of code in global.asax, BundleTable.Bundles.RegisterTemplateBundles();, which requires using System.Web.Optimization; at the top.

执行此操作时,我会看到红色波浪线,上面写着:您是否缺少程序集参考?"当我尝试添加引用并单击对话框中的 .NET 选项卡时,从 A-Z 排序,我没有看到 System.Web.Optimization.

When I do this, I get the red squiggly lines that say, "Are you missing an assembly reference?" When I try and add reference, and click on the .NET tab in the dialog, sort from A-Z, I do not see System.Web.Optimization.

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

How do I add this reference to my project?

推荐答案

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

Microsoft.Web.Optimization 包现已过时.对于 ASP.NET (MVC) 4 及更高版本,您应该安装 Microsoft ASP.NET Web Optimization Framework:

The Microsoft.Web.Optimization package is now obsolete. With ASP.NET (MVC) 4 and higher you should install the Microsoft ASP.NET Web Optimization Framework:

Install-Package Microsoft.AspNet.Web.Optimization

  • 在 App_StartBundleConfig.cs 中创建和配置包:

  • Create and configure bundle(s) in App_StartBundleConfig.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")
            );
        }
    }
    

  • 从 global.asax.cs 中的 Application_Start() 调用 RegisterBundles() 函数:

  • Call the RegisterBundles() function from Application_Start() in your global.asax.cs:

    using System.Web.Optimization;
    
    protected void Application_Start() {
         ...
         BundleConfig.RegisterBundles(BundleTable.Bundles);
         ...
    }
    

  • 在您的 view.cshtml 中包含优化命名空间并呈现包:

  • 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 了解更多信息

    这篇关于如何为 MVC-3-converted-to-4 应用程序添加对 System.Web.Optimization 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-25 07:16