2014-02-10 29 views
80

我有一個ASP.Net MVC項目,我想實現捆綁,但是我可以在互聯網上找到的所有東西都指示我在App_Start中打開BundleConfig.cs - 但是這個文件在我的項目中不存在。我在該文件夾中只有三個文件:FilterConfigRouteConfigWebApiConfig如何將BundleConfig.cs添加到我的項目中?

當我創建解決方案(IIRC它是一個空白的ASP.NET MVC項目開始時)沒有生成Bundle配置。

看起來這應該很容易做到,但我只是簡單的不能弄明白。

P.S.只是爲了向那些沒有仔細閱讀的人澄清,這是從頭開始創建的MVC4/.Net 4.5應用程序。解決方案標記如下。

+0

您找不到它,因爲它只包含在ASP.NET 4.5項目模板中。我假設你正在使用早期版本的ASP.NET。 –

+0

不,ASP.Net 4.5。 – Maverick

+0

[如何爲MVC-3轉換爲4應用程序添加對System.Web.Optimization的引用]的可能重複(http://stackoverflow.com/questions/9475893/how-to-add-reference-to- system-web-optimization-for-mvc-3-converted-to-4-app) – Liam

回答

135

BundleConfig無非是捆綁配置移動到單獨的文件。它曾經是應用程序的啓動代碼的一部分(過濾器,捆綁,使用的路線是在一個類中配置)

要添加此文件,首先您需要將Microsoft.AspNet.Web.Optimization NuGet包添加到您的Web項目:

Install-Package Microsoft.AspNet.Web.Optimization 

然後在App_Start文件夾下創建一個名爲BundleConfig.cs的新cs文件。以下是我在我礦(ASP.NET MVC 5,但它應該與MVC 4工作):

using System.Web; 
using System.Web.Optimization; 

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

      bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
         "~/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 ScriptBundle("~/bundles/bootstrap").Include(
         "~/Scripts/bootstrap.js", 
         "~/Scripts/respond.js")); 

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

然後修改您的Global.asax和呼叫添加到RegisterBundles()Application_Start()

using System.Web.Optimization; 

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
} 

密切相關的問題:使用 「MVC 5」 沒有看到文件How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app

+2

nuget應該添加這樣的樣板版本。 – niico

相關問題