How to add a media attribute to the CSS LINK html tag for the ASP.NET WebResource.axd Http Handler
The ASP.NET WebResource.axd Http Handler is used to serve resources embedded in DLL's.
The LINK html tag is automatically generated by ASP.NET.
I would like to i开发者_StackOverflow中文版ntercept the generation of the LINK html tag for a certain set of embedded CSS from a third party DLL and add a media attribute.
In summary:
I would like to add a Media attribute to the LINK html tag for the ASP.NET WebResource.axd Http Handler.
So this:
<link type="text/css" rel="stylesheet" href="/WebResource.axd?d=XXXXX" />
Appears like this:
<link media="screen and (min-device-width: 481px)" type="text/css" rel="stylesheet"
href="/WebResource.axd?d=XXXXX" />
Cheers
There is a workaround. Firstly, links like this are being added to the Page
's head. Your page must have runat=”server”
in the <head>
tag for the automatic style sheet inclusion. The pages created by the IDE have this setting automatically. So, links being added is a HtmlLink
control type. The idea is to iterate through controls in Page
's header, find HtmlLink
controls and set necessary attribute (or even attributes). I include this into the Page_Load
event:
Page.Header.Controls
.OfType<HtmlLink>()
.ToList()
.ForEach(link =>
{
link.Attributes["media"] = "screen and (min-device-width: 481px)";
});
Before this I had:
<head id="Header">
<title></title>
<link href="App_Themes/MyTheme/main.css"
type="text/css"
rel="stylesheet" />
</head>
and after the result is:
I know, this uses Themes
insted of WebResource.axd
but for the last one the result will be the same.
The latest thing: there may be another links in the page. So it would be good to recognize our links (the links need to be modified). So if there is no id
attribute you could recognize them by href
attribute.
精彩评论