DEV Community

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

Posted on

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.

Top comments (0)