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

后端 未结 8 1235
情深已故
情深已故 2020-12-23 13:47

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

相关标签:
8条回答
  • 2020-12-23 14:36

    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")
    
    0 讨论(0)
  • 2020-12-23 14:36

    Well, it's an ugly hack, but hopefully the team will add a built-in way to do it in the next release.

    This is how I solved it, maintaining the caching string and still being able to add the media attribute to the tag.

    @{
        var cssMediaBundleUrl = BundleTable.Bundles.ResolveBundleUrl("~/stylesheets/mediacss", true);
    }
    <link href="@cssMediaBundleUrl" rel="stylesheet" type="text/css" media="screen" />
    

    Guess I can turn this into an Html helper, will do that later and edit.

    0 讨论(0)
提交回复
热议问题