Pages

Monday, October 10, 2016

ASP.NET Bundling Rendering Media Attribute on Link Tag


Reading this post about making a site mobile friendly, I noticed ASP.NET bundling doesn’t render the media attribute on the HTML link tag.

Quick search, found a solution at stackoverflow:


From the posted solution, I changed it a little.

<!DOCTYPE html>

 

<html class=" js ">

<head>

    <title></title>

    @Styles.Render("~/Styles/Base")

</head>

<body class="master-body" id="index">

To…

@using com.dogPedaler.Models

 

<!DOCTYPE html>

 

<html class=" js ">

<head>

    <title></title>

    @Styles.RenderFormat(BundlesFormats.Screen, "~/Styles/Base")

</head>

<body class="master-body" id="index">



The change includes the namespace to a class with a static property call Screen:

@using com.dogPedaler.Models

The Created Class

namespace com.dogPedaler.Models

{

    public class BundlesFormats

    {

        private const string template = @"<link href=""{0}"" ";

        private const string template2 = @"rel=""stylesheet"" type=""text/css"" media=""{0}"" />";

        public static string Print

        {

            get

            {

                return template + String.Format(template2, "print");

            }

        }

        public static string Screen

        {

            get

            {

                return template + String.Format(template2, "Screen");

            }

        }

        public static string HandHeld

        {

            get

            {

                return template + String.Format(@"rel=""stylesheet"" type=""text/css"" media=""print"" />", "handheld");

            }

        }

 

    }

}

Notice three properties

  • print
  • Screen
  • handheld

There is a reason why the “S” is capitalized. This is a hack for Windows mobile browsers and can be found at:

https://perishablepress.com/the-5-minute-css-mobile-makeover/
 
 
This post is for the purpose of my notes only and sometimes a rant.

“I invented nothing new. I simply assembled the discoveries of other men behind whom were centuries of work. Had I worked fifty or ten or even five years before, I would have failed. So it is with every new thing. Progress happens when all the factors that make for it are ready and then it is inevitable. To teach that a comparatively few men are responsible for the greatest forward steps of mankind is the worst sort of nonsense.”

Henry Ford

No comments:

Post a Comment