asp.net-mvcasp.net-mvc-4bundling-and-minificationasp.net-optimization

Make ASP.NET bundling specify media=screen for CSS bundle


I'm just trying out ASP.NET 4.5 bundling and minification, and ran into an issue.

I've got around 10 css files, of which 2 were originally referenced in the layout using the attribute media="screen".

Since the syntax for adding a css to the bundle does not let you specify that such attribute should be added (makes sense, since the attribute would apply for the whole bundle), I was hoping to see an overload of @Styles.Render that would allow me to specify html attributes, like in other Html helpers, but there is none.

There is an ugly solution, in which since I know the url of the bundle created, i could just craft the tag myself, but I'd lose the caching mechanism that is handled by ASP.NET by allowing it to render the tag itself.

Is there a way to do this, am I missing something? Or is this just an oversight of the design team?


Solution

  • I've found a more elegant solution.

    I'm using the Styles.RenderFormat(format, bundle).

    I have a BundlesFormats class with a property called PRINT and I use it like so:

    public class BundlesFormats
    {
        public const string PRINT = @"<link href=""{0}"" rel=""stylesheet"" type=""text/css"" media=""print"" />";
    }
    

    And in the cshtml:

    @Styles.RenderFormat(BundlesFormats.PRINT, "~/bundles/Content/print")