views:

107

answers:

1

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 IE8 except when IE8 is running in Compatability 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; }

IE8 Compatability 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  A: 
<%@ Page Language="C#" AutoEventWireup="true" ContentType="text/css"%>
@import "<%= "myCss.css" %>";
Dmytrii Nagirniak
Much cleaner syntax, but the result is the same. Fails in IE8 Compat view.
kristian
What exactly fails? If you open the file in IE what do you see?
Dmytrii Nagirniak
When viewing my web page in IE8 compat view, I see it with no styles applied. When I disable compat view (i.e. when viewing my web page with default IE8), I see it with css styles applied.
kristian
My question was: What do you see if you open the page we have implemented here in IE (Comp.View)? Not the page that uses your dynamic css.
Dmytrii Nagirniak
I see exactly the same thing, regardless of whether my page contains the code described in the question, or the code described in this answer.
kristian
My question was: What do you see if you **open the page we have implemented here** in IE (Comp.View)? Not the page that uses your dynamic css
Dmytrii Nagirniak
Sorry Dmitriy, I'm not being deliberately obtuse, I don't understand what you are referring to when you say **the page we have implemented here**. Do you mean a page containing the code you supplied in your answer? Something else?
kristian
@kristian, sorru if it sounded offensive, but you just did not answer my question twice. I wanted to know what you see if you open the page with the code we are discussing here (so we should see the CSS with @import rule).Anyway, I updated the answer and it should work now for you.
Dmytrii Nagirniak