【问题标题】:Adding the new ASP.NET Web Optimization framework to MVC4 projects after manually upgrading them手动升级后将新的 ASP.NET Web 优化框架添加到 MVC4 项目
【发布时间】:2012-08-18 16:00:50
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:
    • 右键单击 References 然后 Manage NuGet Packages 并添加“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

    【讨论】:

      【解决方案2】:

      是 按照以下步骤打包和缩小 JS 和 CSS:

      • 首先打开包管理器控制台并运行命令,选择您的 Web 应用作为项目。

      安装包 Microsoft.AspNet.Web.Optimization

      • 进入global.asax右键查看代码

      • 粘贴以下代码:

        public static void MinifyJavaScriptAndCSS()
        {
           var scripts1 = new ScriptBundle("~/bundles/customJSBundle");
           scripts1.Include("~/Scripts/script1.js");
           scripts1.Include("~/Scripts/script2.js");
           BundleTable.Bundles.Add(scripts1);
        
            //Bundle Css
            var css1 = new StyleBundle("~/bundles/customCSSBundle");
            css1.Include("~/Styles/style1.css");
            css1.Include("~/Styles/style2.css");
            BundleTable.Bundles.Add(css1);  
         }
        
      • 在 Application_Start() 中调用它

         protected void Application_Start()
        {
            ApplicationHelper.MinifyJavaScript();
         }
        
      • 转到视图/共享中的 _Layout.cshtml

      • 在head中添加一行

        @ViewBag.Title - 我的 ASP.NET 应用程序 @Styles.Render("~/bundles/customCSSBundle")

      • 在关闭body标签之前添加这个

         //your code
          @Scripts.Render("~/bundles/customJSBundle")
         </body>
        
      • 如果在 web.config 中设置编译调试=true,文件将不会被捆绑。 如果将其设置为 false,则将捆绑文件。

      【讨论】:

        猜你喜欢
        • 2012-09-04
        • 2018-06-02
        • 1970-01-01
        • 1970-01-01
        • 2013-12-22
        • 1970-01-01
        • 2013-04-30
        • 2014-02-16
        • 2020-01-31
        相关资源
        最近更新 更多