2014-02-19 83 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并排除常用包 –