手动升级后,将新的ASP.NET Web Optimization框架添加到MVC4项目中

Ed *_*sen 13 asp.net asp.net-mvc asp.net-mvc-4 bundling-and-minification asp.net-optimization

使用这些说明手动将ASP.NET MVC项目升级到MVC4后,如何在MVC4中设置ASP.NET Web优化框架的新CSS和JavaScript资产捆绑和最小化功能?默认模板已全部设置,但您如何手动完成?

Ed *_*sen 31

  • 右键单击References然后管理NuGet Packages并添加"Microsoft.AspNet.Web.Optimization"(或输入Install-Package Microsoft.AspNet.Web.OptimizationNuGet控制台).
  • 在Web.config文件中,添加以下内容<system.webServer>,允许使用无扩展名URL提供缩小的捆绑包.
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
Run Code Online (Sandbox Code Playgroud)
  • 在App_Start文件夹中,添加一个名为BundleConfig.cs的新类.它应该看起来像这样:
using System.Web;
using System.Web.Optimization;

namespace MvcApplication1
{
    public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                "~/Scripts/jquery-ui-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.unobtrusive*",
                "~/Scripts/jquery.validate*"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

            bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                        "~/Content/themes/base/jquery.ui.core.css"));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 编辑上面的内容以添加所需的脚本和样式表包,然后将以下行添加到Global.asax.cs中的using部分和Application_Start:
//using section
using System.Web.Optimization;

//Application_Start
BundleConfig.RegisterBundles(BundleTable.Bundles);
Run Code Online (Sandbox Code Playgroud)
  • 用_Layout.cshtml替换你的CSS和JavaScript和标签,调用@Styles.Render("~/Content/css")@Scripts.Render("~/bundles/jquery"),用你添加到BundleConfig.cs的包的名称替换参数.确保不要将任何捆绑包命名为项目中的文件夹.

您现在应该全部准备好了 - 请阅读以下有关如何使用完整功能集的信息:http://www.asp.net/mvc/overview/performance/bundling-and-minification