Search code examples
asp.net-mvcasp.net-mvc-5bundling-and-minificationscriptbundlestylebundle

is it possible to include Scripts and Styles in same Bundle ?(asp.net mvc5)


Attempting to use css and js files with same virtualpath bundle name
1 - is it possible ? (tried:but failed. cant define same virtual path name both for script and style)
2 - it it possible to builtup a ScriptAndStyleBundle together included with a mixed bundle ?

Just because I wantto use same name both for css and js.

//in BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/doMenu")
            .Include("~/Plugins/doMenu/files/js/doMenu.js")
            );

        bundles.Add(new StyleBundle("~/bundles/doMenu")
            .Include("~/Plugins/doMenu/files/css/doMenu.css")
            );

//in _PartialLayoutMenu.cs
@Scripts.Render("~/bundles/doMenu")
@Styles.Render("~/bundles/doMenu")


result is:

<!--MENU LAYOUT-->
<script src="/Plugins/doMenu/files/css/doMenu.css"></script>

<link href="/Plugins/doMenu/files/css/doMenu.css" rel="stylesheet"/>
<!--/MENU LAYOUT-->

any clue ? or I want such a useless mind ?(thanks)


Solution

  • I'm using Cassette, which does support this feature.

    Configuration:

    bundles.Add<ScriptBundle>(BundleNames.Grid,
                              new[] {"~/Scripts/Grid.js"},
                              bundle => bundle.AddReference("~/" + BundleNames.Base));
    
    bundles.Add<StylesheetBundle>(BundleNames.Grid,
                                  new[] {"~/Content/Grid.less"});
    

    BundleNames is a helper class I have with constant strings.

    Reference bundles in views:

    @{
        Bundles.Reference(BundleNames.Grid);
    }
    

    As you'd expect, this will include all CSS and JS, in the correct order.

    I'd also mention that I've started using Cassette before ASP.Net had bundle management and I'm very happy with it. I didn't find any interesting feature ASP.Net had that I'm missing.