2014-02-19 82 views
0

我已經使用System.Web.Optimization & WebGrease DLL使用下面的代碼在我的項目中自動執行捆綁包和minifiy javascript和css文件。使用System.Web.Optimization&WebGrease在asp.net mvc中自動化minfication而不捆綁

using System.Web.Optimization; 
using WebGrease; 

protected void Application_Start() 
{ 
    RegisterBundles(BundleTable.Bundles); 
    BundleTable.EnableOptimizations = false; 

} 
public static void RegisterBundles(BundleCollection bundles) 
{ 
    bundles.Add(new ScriptBundle("~/Content/Script/Common/").Include("~/Content/Scripts/PTax/Common/ViewPort.js", 
                     "~/Content/Scripts/PTax/Common/jquery.rotate.js", 
                     "~/Content/Scripts/PTax/Common/jquery.loupe.js", 
                     "~/Content/Scripts/PTax/Common/Accordion.js", 
                     "~/Content/Scripts/PTax/Common/Progress.js", 
                     "~/Content/Scripts/PTax/Common/AjaxGlobalHandler.js", 
                     "~/Content/Scripts/PTax/Common/DialogBox.js", 
                     "~/Content/Scripts/PTax/Common/Common.js", 
                     "~/Content/Scripts/PTax/Common/DateValidations.js")); 

} 

在我的項目中一些模塊只有一個JavaScript文件。我不需要將其與任何其他文件捆綁在一起。所以我只需要它minifiy。我怎樣才能做到這一點。

+0

你可以再壓縮JS的編輯時間WebEssentials VS擴展(http://vswebessentials.com/) – trailmax

回答

0

您可以創建一個新包,(這是正常創建多個包)

bundles.Add(new ScriptBundle("~/bundles/get").Include("~/Scripts/app/home.js")); 

然後渲染腳本在必要的視圖

@Scripts.Render("~/bundles/home") 
+0

我可以在相應的視圖本身做自動縮小而不需要像在global.asax文件中說的那樣添加行。 – Harishankaran

+0

不是我所知道的,webgrease需要你指定的文件,並將其縮小並將它們合併爲1,如果不指定文件,webgrease如何知道捆綁和縮小什麼? 在你的一些觀點中,應該有'〜/ Content/Script/Common /'?您也可以在視圖的文件夾中指定另一個_Layouts.cshtml並排除常用包 –