0

I want to define a few colours in one location and use functions to generate tints of these colors and render the generated colors in the CSS files.

I am moving from working on Ruby on Rails to ASP.NET. On my previous RoR project I had a 5-color color scheme set as Ruby variables of RGB arrays. I then used functions in the .css.erb file to generate a range of tints and shades of the color scheme colors throughout the generated .css stylesheet.

What's the cleanest way to do this in ASP.NET MVC 5?

user1107685
  • 441
  • 1
  • 4
  • 13

1 Answers1

1

You can easily render CSS with C# code in ASP.Net (either ASP.Net MVC or WebForms or raw ASP.Net) as it is just a file with particular content type.

One option is just render CSS inline on the page if you need just couple overrides.

For ASP.Net MVC to create separate file would be generate text in controller and than return it as Content - Display contents of Text File in MVC3 Razor. You can also make view to render no HTML tags and instead just CSS to be able to use all Razor constructs. Don't forget to add route and you may need to add <modules runAllManagedModulesForAllRequests="true" /> Meaning if using ".css" extension for files.

Note: depending on what you actually want to do using SASS/LESS to use "variables" in CSS may be better. It will not let you to get "colors for current user" scenarios, but allow to statically construct consistent CSS (like "button color must be the same and easy to change once across all of my CSS files").

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179