Search code examples
c#asp.net-mvcbundling-and-minification

Add multiple Bundles inside a single Bundle in C# MVC


For Example I Want to Create Bundles like this

/*These are my Two separate Bundles*/

bundles.Add(new StyleBundle("~/Content/MYBundle1").Include(
           "~/Content/css/style.css"));

bundles.Add(new StyleBundle("~/Content/MYBundle2").Include(
           "~/Content/css/media.css"));

/*Now i want to use above two bundles as a single bundle */

bundles.Add(new StyleBundle("~/Content/AllPageBundles").Include(
           "~/Content/MYBundle1",
           "~/Content/MYBundle2")

Also I want to ask that can i add reference of any file inside a bundle that is physically exist at any site server For Example:I want to add google fonts file inside a bundle like i write bellow

bundles.Add(new StyleBundle("~/Content/MYBundle1").Include(
               "http://fonts.googleapis.com/css?family=Open+Sans:300"));

Solution

  • For multi-bundles you can write like:

    For directories:

     bundles.Add(new Bundle("~/js/core").IncludeDirectory(@"~/Scripts/Infrastructure/JQuery", "*.js")
                                                         .IncludeDirectory(@"~/Scripts/Infrastructure/Knockout", "*.js")
                                                         .IncludeDirectory(@"~/Scripts/Infrastructure", "*.js"));
    

    For files:

     bundles.Add(
                    new Bundle("~/js/kendo").Include("~/Scripts/kendo/kendo.core.min.js")
                                            .Include("~/Scripts/kendo/kendo.data.min.js")
                                            .Include("~/Scripts/kendo/kendo.binder.min.js")
                                            .Include("~/Scripts/kendo/kendo.calendar.min.js")
    

    For url try this code:

    var jqueryCdnPath = "http://fonts.googleapis.com/css?family=Open+Sans:300";
    bundles.Add(new ScriptBundle("myfoobundle", jqueryCdnPath).Include("~/Scripts/jquery-{version}.js"));