Asp.net

手動升級後將新的 ASP.NET Web 優化框架添加到 MVC4 項目

  • September 9, 2015

使用這些說明手動將 ASP.NET MVC 項目升級到 MVC4 後,您如何在 MVC4 中設置新的 CSS 和 JavaScript 資產捆綁以及 ASP.NET Web 優化框架的最小化功能?預設模板已全部設置好,但是您如何手動完成呢?

  • 右鍵點擊 References 然後 Manage NuGet Packages 並添加“Microsoft.AspNet.Web.Optimization”(或Install-Package Microsoft.AspNet.Web.Optimization在 NuGet 控制台中鍵入)。
  • 在您的 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

引用自:https://stackoverflow.com/questions/12086518