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

在将ASP.NET MVC项目手动升级到MVC4 using these instructions后,如何在MVC4中设置ASP.NET Web Optimization Framework的新CSS和JavaScript资产捆绑和最小化功能认模板全部设置完成,但是如何手动执行?

解决方法

>右键单击“引用”,然后管理NuGet软件包,并添加“Microsoft.AspNet.Web.Optimization”(或在NuGet控制台中键入Install-Package Microsoft.AspNet.Web.Optimization).
>在您的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,OPTIONS" modules="IsapiModule" scriptprocessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

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

>编辑上面的内容添加所需的脚本和样式表包,然后在Global.asax.cs中的using部分和Application_Start中添加以下行:

//using section
using System.Web.Optimization;

//Application_Start
BundleConfig.RegisterBundles(BundleTable.Bundles);

>将_Layout.cshtml中的CSS和JavaScript和标签替换为@ Styles.Render(“〜/ Content / css”)和@ Scripts.Render(“〜/ bundles / jquery”),将参数替换为您添加到BundleConfig.cs的捆绑包.确保不要将与项目中的文件夹相同的任何包命名.

您现在应该全部设置 – 请阅读如何使用完整的功能集:http://www.asp.net/mvc/overview/performance/bundling-and-minification

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...