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?

4
  • I am not a FED but doesn't SASS handle this already? Commented Jan 22, 2018 at 17:48
  • Yeah. You want LESS or SASS Commented Jan 22, 2018 at 17:48
  • The cleanest way is not to do it neither with c# nor Ruby. There are CSS pre-processors exactly for this (SASS, LESS...) Commented Jan 22, 2018 at 17:49
  • I looked into SASS - yes, you're right - I did not realise SASS had color functions, I'd only previously used it for nesting. If one of you wants to give it as an answer I can accept. Commented Jan 23, 2018 at 8:42

1 Answer 1

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").

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - SASS with color functions looks like a good fit for the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.