How do we add Bundling and Minification to a project that was upgraded from Visual Studio 2010 project

∥☆過路亽.° 提交于 2019-12-11 08:08:27

问题


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?

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:


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.

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

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"));
    }
}

Step 4: Load on ApplicationStart in global.asax:

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

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>

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.



来源:https://stackoverflow.com/questions/21642509/how-do-we-add-bundling-and-minification-to-a-project-that-was-upgraded-from-visu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!