且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们如何将捆绑和缩小添加到从Visual Studio 2010项目升级的项目中

更新时间:2022-12-07 20:24:29

步骤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.