2

Setup: MVC 5, .NET 4.7.2, Web Compiler extension in Visual Studio 2019.

What I want to do is dynamically set SASS variables based on values stored in a database. I am building a small site that will be used by multiple companies and I want to be able to change the styles (mainly colors) based on data loaded from the database. What I've done in the past is to just dynamically generate CSS using a generic handler, but I'd like to use SASS on this project and I don't know if there is a way to set SASS variables from .NET code.

I tried to use an @import and a generic handler, but that did not work because web compiler is trying to pull that code in and create the CSS on file save. It could be I am going about this completely the wrong way, so if there is another idea I'm open to it.

I read this post Set a SASS variable depending that I could potentially use, by setting all of the colors in the SCSS file and then set the body class dynamically using ViewBag. It would not be my preferred approach though.

4
  • Wich SASS implementation are you using? Wikipedia lists none for C#/.NET, only C++. en.wikipedia.org/wiki/… Commented Nov 14, 2019 at 15:24
  • 1
    In the common scenarios, you can't set a SASS variable from .NET code at run time because the SASS file gets compiled to CSS at compile time. You CAN change this to do the SASS compile at run time, but it isn't a trivial thing to do. Typically in the past I have either changed the stylesheet dynamically (like selecting a theme), or if there are only a few changes, then use the method you suggested above where you put a class on an element that is then used to trigger the style changes. Commented Nov 14, 2019 at 15:24
  • @Christopher It is the web compiler extension -- marketplace.visualstudio.com/… Commented Nov 14, 2019 at 15:25
  • @RobertMcKee, that is what I thought, thanks! Commented Nov 15, 2019 at 16:50

0

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.