Click here to Skip to main content
Click here to Skip to main content

Dynamic CSS using Razor Engine

By , 22 Mar 2011
 

Introduction

I was playing around with Razor and CSS trying to start a website and searching a way to parameterize the CSS values for my site, because I always make a mess in the CSS :). After a few hours of testing and thinking, I wrote this code. I hope this will be helpful. This code helps to use a dynamic CSS file generated with Razor engine. You can use all the power of Razor to generate the CSS, for example, you can use variables, conditionals, functions. I know that http://lesscss.org/ exists, but I think that using Razor is a more standard way because you are using all the power of the View Engine. Of course, http://lesscss.org/ is more complex and complete, but you can evaluate this approach.

Background

The basic idea is to generate the CSS file dynamically, but not the entire file, just the important values, like the primary colors, fonts, all settings that define your site. Using this approach, you can have one (or more) CSS file and use this like a template and define variables and reuse in all CSS files.

Using the Code

The use is very simple With NuGet (http://nuget.codeplex.com/) installed, the first thing you have to do is install the package of razorengine. To do that, go to Tools > Library Package Manager > Package Manager Console and write in console:

install-package razorengine

After that, create an ASP.NET MVC 3 Web Application Project.

and select:

Go to _Layout View:

and change the CSS link:

Then add a Controller named Home, add a View named Index. You can create any Controller and View. I suggest that because they are the default MVC. Then create the controller that will create the CSS dynamically. Add the controller 'Style'. You can choose any name, but note that the name of controller must match with the 'link href="@Url.Action("Index", "Style")"' used in _Layout View.

The Style Controller will only have an Action, and will be the default Action Index. This Action returns the CSS generated in a string:

Like you can see, this Action will load the CSS file '~/Content/Site.css'. You can change or even add more complex logic to decide which CSS file loads, or whatever you want. And after load, the file parses with Razor.Parse(). This will parse the Razor template and generate a result content, in this case a CSS. Now let me show you what is inside the Site.css, here comes the good part. :)

In the CSS, you just write Razor Code! You can define variables or whatever that Razor allows you, and then use in any part of the CSS:

and reuse, and reuse, and reuse, just use a Razor syntax '@{}':

Well, that's all. I hope this will be helpful in some way if you need to use dynamic CSS. I know that many improvements are needed, but this is just a beginning. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

asanoguera
Software Developer
Argentina Argentina
Member
I born in 1977, I Studies computer science from age 12, at 18 he was working in a company that developed solutions for clinical and biochemical laboratories in QuickBasic and Btrieve, at 21 I started to work in a consulting firm that developed solutions for companies in QBX, VB5 and MSAccess, I also worked as a senior programmer for 5 years in a government agency analyzing and building management systems and statistics for decision making in VB6, ASP and MS-SQL 2000, independently developed several projects and also work for companies in the abroad in ASP / ASP.NET and MS-SQL 2000, I currently developing desktop applications and Web applications in MVC 3, Razor, Entity Framework, jQuery, VB.NET and C# and MS-SQL 2008

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThat's brilliant PinmvpSacha Barber18 Jun '12 - 22:57 
I am using LESS, but I really like how simple this is. Great. 5 from me
Sacha Barber
  • Microsoft Visual C# MVP 2008-2012
  • Codeproject MVP 2008-2012
Open Source Projects
Cinch SL/WPF MVVM

Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

QuestionHow can i post a value from controller to css file?? Pinmember-tamilan2-27 Jan '12 - 8:52 
How can i post a value from controller to css file?? I want to get value from client side and post that to css by controller. Viewbag is not working, please help me!!!!!! Frown | :( Frown | :( :(
AnswerRe: How can i post a value from controller to css file?? Pinmember-tamilan2-27 Jan '12 - 23:47 
AnswerRe: How can i post a value from controller to css file?? Pinmemberasanoguera29 Jan '12 - 12:52 
GeneralRe: How can i post a value from controller to css file?? Pinmember-tamilan2-30 Jan '12 - 3:30 
GeneralMy vote of 3 PinmemberDhol Gaurav5 Jan '12 - 19:39 
good example for MVC beginner
GeneralRe: My vote of 3 Pinmemberasanoguera29 Jan '12 - 12:56 
Questionmaybe some less? PinmemberSeishin#25 Mar '11 - 3:01 
There's a VS add-in called >Chirpy< .
It adds (among other cool features) support (syntax highlighting, code folding) for .less files. You can find more on less >here<
life is study!!!

GeneralLittle Improvement PinmemberRemi BOURGAREL23 Mar '11 - 4:42 
Because a css file will be called a lot of time and won't change a lot between two calls, you can add an OutputCache
 
[OutputCache(Duration=10, VaryByParam="none")]
 

But nice and simple article.
GeneralRe: Little Improvement Pinmemberasanoguera23 Mar '11 - 7:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 22 Mar 2011
Article Copyright 2011 by asanoguera
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid