DEV Community

Cover image for Dynamic CSS with ASP.NET - in one line of code
Adam K Dean
Adam K Dean

Posted on

1

Dynamic CSS with ASP.NET - in one line of code

Whether you want a separate sheet for each browser, or you want to track when people print a page from your site (as is my case), how do you get code to run in a CSS file? You can't. How do you get an ASPX file to serve up CSS? Well, you can't. But like House MD, I have the answer and I'll tell you eventually.

What you want to do is create an ASPX file, and in the Page_Load event, write whatever tracking or logic code you want, then simply redirect the page without any output.

protected void Page_Load(object sender, EventArgs e)
{
    // code here
    Response.Redirect("style.css", true);
}

And there you have it, Dynamic CSS with ASP.NET - in one line of code.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay