ASP.NET: Returning CSS to client

I wasn’t a heavy back end guy but I’ve got my hands on some server control now, which returns CSS to another aspx page that expects the CSS in a <link/> tag in the page head. Something like this:

<link rel="stylesheet" type="text/css" href="Style.aspx?id=15" />

And code file of the Style.aspx page is as simple as:

string style = string.Empty;
// ...
// Grab style from DB.
// ...
Response.Clear();
Response.Write(style);
Response.End();

I realised that none of the browsers but IE recognises the styles correctly.

After a bit of trial and error + research, I found out that it was because I didn’t set the web response header correctly before return it.

What I was missing is just one line of code before calling the end for the response:

Response.AddHeader("Content-Type", "text/css");

I also found that you can’t use Response.Headers.Add("", "") to add the header entry, but didn’t go deeper looking into the reason though 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s