我们如何将捆绑和缩小添加到从Visual Studio 2010项目升级的项目中 [英] How do we add Bundling and Minification to a project that was upgraded from Visual Studio 2010 project

查看:68
本文介绍了我们如何将捆绑和缩小添加到从Visual Studio 2010项目升级的项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用Visual Studio 2010开发的旧版网站项目升级为Visual Studio 2013中的Web应用程序项目。现在,我想向该应用程序添加一些MVC组件,包括捆绑和缩小。但是App_Start文件夹丢失。我是否需要安装任何可以创建App_Start文件夹并添加捆绑和缩小功能的NuGet软件包?

I upgraded a legacy website project developed using Visual Studio 2010 to a Web Application project in Visual Studio 2013. Now I want to add some MVC components to this application including Bundling and Minification. But the App_Start folder is missing. Is there any NuGet package I need to install that would create the App_Start folder and add the 'Bundling and Minification' feature?

例如,该项目也缺少内容文件夹,但是当我通过NuGet软件包管理器安装jQuery UI 1.10.4软件包时,它创建了Content文件夹并向其中添加jQuery UI css文件。

For instance, this project was also missing the 'Content' folder, but when I installed the jQuery UI 1.10.4 package via NuGet package Manager, it created the Content folder and add jQuery UI css files to it.

推荐答案

步骤1:(通过GUI)安装捆绑和压缩。 工具>图书包管理器>管理解决方案的NuGet程序包。在线搜索捆绑,第一个结果应该是 Microsoft ASP.NET Web优化框架。安装。

Step 1: Install Bundling and Minification (via the GUI). Tools > Library Package Manager > Manage NuGet Packages For Solution. Search Online for "bundling" and the first result should be Microsoft ASP.NET Web Optimization Framework. Install that.

步骤2:右键单击您的Web项目并添加新文件夹,并将其命名为 App_Start

Step 2: Right-click your Web Project and Add New Folder and call it App_Start.

步骤3: App_Start 中添加新类称为 BundleConfig.cs 。看起来可能像这样:

Step 3: Add a new class in App_Start called BundleConfig.cs. It could look like this:

using System.Web.Optimization;

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));
    }
}

步骤4:在global.asax中的ApplicationStart上:

Step 4: Load on ApplicationStart in global.asax:

void Application_Start(object sender, EventArgs e)
{
     BundleConfig.RegisterBundles(BundleTable.Bundles);
}

步骤5:添加到MasterPage的页脚中。我通常会这样:

Step 5: Add to footer of MasterPage. I usually do like this:

        <%: System.Web.Optimization.Scripts.Render("~/bundles/jquery") %>
        <asp:ContentPlaceHolder ID="MyScripts" runat="server"></asp:ContentPlaceHolder>
    </body>
</html>

这样,jQuery就会在< / body> 标记,您可以将所有内联脚本放入 ContentPlaceHolder 中,该脚本在引用主脚本后呈现。

That way, jQuery loads near the </body> tag and you can put all inline scripts in a ContentPlaceHolder that renders after your main script references.

这篇关于我们如何将捆绑和缩小添加到从Visual Studio 2010项目升级的项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆