asp.net - 手动升级后将新的 ASP.NET Web 优化框架添加到 MVC4 项目

标签 asp.net asp.net-mvc asp.net-mvc-4 asp.net-optimization bundling-and-minification

手动将 ASP.NET MVC 项目升级到 MVC4 后 using these instructions ,然后如何在 MVC4 中设置 ASP.NET Web 优化框架的新 CSS 和 JavaScript Assets 捆绑和最小化功能?默认模板已经设置了所有这些,但是您如何手动完成呢?

最佳答案

  • 右键单击“引用”,然后单击“管理 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,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>
    
  • 在您的 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

    关于asp.net - 手动升级后将新的 ASP.NET Web 优化框架添加到 MVC4 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12086518/

    相关文章:

    C# 更改表并以编程方式添加列 ASP.Net 和 SQL Server

    mysql - 需要对代码进行一些修改

    asp.net - DbContext 在 ActionFilterAttribute 中处置,不会在后续请求中重新加载

    asp.net-mvc - 使用 ASP.NET MVC 重定向到 FormsAuthentication 登录页面时显示有用的消息

    json - 牛顿软件 JSON

    c# - 设置单元测试以使用 Unity 测试 Controller

    c# - 如何找到发送和接收数据之间耗时?

    asp.net-mvc - 我的日志框架永远与我的应用程序绑定(bind)在一起!

    mysql - 使用 FirstOrDefault() 时 Entity Framework 更新多条记录

    asp.net-mvc - 向 Controller 构造函数注入(inject)服务时出现不一致的可访问性参数类型错误