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

2.7k views Asked by At

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)

2

There are 2 answers

3
Kobi On BEST ANSWER

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.

1
Darin Dimitrov On

Bundle names should be unique within the both styles and scripts. And since they are entirely different types you cannot mix them into a single bundle because then you wouldn't know how to reference it in the DOM: whether you should use a <script> or a <style> tag. Unfortunately what you are asking for is not possible.