Serving CSS from a .aspx page doesn't work with Internet Explorer 8 in Compatibility View

1.2k Views Asked by At

I have a .aspx page which I want the browser to treat like a regular .css file. The code in the .aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server" type="text/C#">

protected void Page_Init(object sender, EventArgs e)
{
    Response.ContentType = "text/css";
}

</script>

@import url(<%= "myCss.css" %>)

This works fine in Chrome, Firefox 3.5.5, and Internet Explorer 8 except when Internet Explorer 8 is running in Compatibility View when the CSS does not appear to be affecting the page at all - i.e. I see my web page with no styles applied.

How do I serve CSS from .aspx in a way that will work across all (most) browsers?

EDIT: When I change the actual CSS being served from

@import url(<%= "myCss.css" %>)

to

div { background-color: yellow; }

Internet Explorer 8 Compatibility view seems to work perfectly.

EDIT: After dismantling and rebuilding my page a couple of times, everything seems to work but I'm still not sure why it works. I'm going to mark Dmitriy's answer as correct anyway because I've changed my code to his much nicer version.

1

There are 1 best solutions below

8
On BEST ANSWER
<%@ Page Language="C#" AutoEventWireup="true" ContentType="text/css"%>
@import "<%= "myCss.css" %>";