0

i want to save a color in the web.config file and use it in the CSS file, to use it for different web apps. e.g.: Web app 1 has a blue design, Web app 2 has a green design ... I the web.config it looks like this:

<add key="name" value=" #000000"/>

My question is, how can i get this value in the CSS file.

3
  • You can't. css files are static and not generated by the server. Commented Feb 27, 2018 at 12:42
  • What you CAN do is put a style tag in the razor template and add this rule there. Or you can use SCSS, but you'll still not be able to use values from web.config. Commented Feb 27, 2018 at 12:43
  • When you run into this problem, it's best to consider making Master pages and then referencing them dynamically from your app. One of the benefits to CSS is the ability to change aspects of design without changing your back end or individual markup files and recompiling, and bypassing that tends to be a maintenance regret later. Commented Feb 27, 2018 at 13:20

2 Answers 2

2

I also got an error when I tried Tim Gerhard's solution. (I can't enter this as a comment to that answer because I'm a relatively new user.) After several iterations, I made it work (in an actual inline style), like this:

<!-- In web.config -->
<appSettings>
    <add key="PageTitleStyle" value="style='color:red'"/>
</appSettings>

<!-- In the .aspx file -->
<span <asp:Literal runat="server" Text="<%$ AppSettings:PageTitleStyle %>" /> >
      Page Title Text Here</span>

Note that in my actual application, I also get the page title text itself from Web.config, using the same syntax with a separate "add key" string.

It would also be valid, and often considered preferable, in an "internal style sheet" form as Tim illustrates. In fact, re-reading your question, I think you'll want to do it that way.

I tried it with just the string "red" in the key, but it wouldn't let me splice that into the inline style tag after "color:". So I ended up with the entire style string in Web.config. I didn't try it in an "internal style sheet" context, but it's just different enough that it might work.


Supplemental information - In answer to Tim's question about the errors I received trying his construct (using Visual Studio 2013 and .Net 4). First I got:

The expression prefix 'ConfigurationManager.AppSettings' was not recognized. Please correct the prefix or register the prefix in the section of configuration.

When I removed "ConfigurationManager." then I got:

The expression '<%$ AppSettings['PageTitleStyle'] %>' is invalid. Expressions use the syntax <%$ prefix:value %>.

So then I changed the square bracket construct to use a colon. Then I got ("blockquote" doesn't work right with this one, so I'm using "code sample" formatting):

Literal expressions like '<%$ AppSettingsa:PageTitleStyle %>' are not allowed.
Use <asp:Literal runat="server" Text="<%$ AppSettingsa:PageTitleStyle%>" /> instead.

So that led me to the working syntax above.

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

Comments

0

You will need to use inline stylings to set webconfig values in your css file. Like this:

<style type="text/css">
        .yourClass {
            color: <%= ConfigurationManager.AppSettings["YOURKEYNAME"] %>;
        }
</style>

You could add it in your razor Master template for example.

You can read more about this here: https://forums.asp.net/t/1349824.aspx?How+to+set+CSS+color+settings+from+web+config

1 Comment

I've already found this. But it doesn't seem to work with this line of code.

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.