Leveraging .Net ConfigurationManager.AppSettings in Cascading Style Sheets

I am facing an issue where I have a specific color key in my AppSettings for the company's brand color, which needs to be applied to a CSS class. The challenge is how to incorporate this key into my stylesheet. Is there a way to access the ConfigurationManager within a .css file or any alternative method to achieve this?

Web.config configuration with color keys:

    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="CorpColorBlue" value="#004B85"/>
        <add key="CorpColorGreen" value="#009467"/>
        <add key="CorpColorOrange" value="#E98300"/>
    </appSettings>

CSS Class requiring color application (brackets for clarity):

.thead-company {
    color: white;
    background-color: [COMPANY-BLUE];
    border-color: [COMPANY-BLUE];
}

Currently, I am directly accessing ConfigurationManager in my views which leads to redundant code and complications when colors are updated. What alternatives can I explore?

Current approach:

...
@{
    var companyBlue = ConfigurationManager.AppSettings["CorpColorBlue"];
}
...
<table class="table table-hover table-bordered table-striped">
    <thead class="thead-organization" style="background-color:@companyBlue; border-color:@companyBlue">
...

Answer №1

After a helpful suggestion from user @Pete in the comments, I came across this informative question, which provided the solution I had been searching for. To eliminate duplicate code, I decided to follow the advice given in the accepted answer and implement it as shown below:

Controller:

public ContentResult GetCompanyColorStyle()
        {
            var builder = new StringBuilder();

            builder.Append(".thead-company-primary");
            builder.Append(" { ");
            builder.Append(string.Format("{0}: {1}; ", "background-color", System.Configuration.ConfigurationManager.AppSettings["CorpColorPrimary"]));
            builder.Append(string.Format("{0}: {1}; ", "border-color", System.Configuration.ConfigurationManager.AppSettings["CorpColorPrimary"]));
            builder.AppendLine("}");

            return Content(builder.ToString(), "text/css");
        }

_Layout.cshtml:

<link href="@Url.RouteUrl(new { controller = "Home", action = "GetCompanyColorStyle" })" rel="stylesheet" type="text/css" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the dynamic attributes of JSON within the C# environment of .NET

Upon querying an API, I receive the following JSON format: { "7407": { "survey_id" : "406", "device_id" : "1", "response_time" : "2013-10-10 16:14:01", "timezone" : "0", "language_id" : "en", "response_i ...

Tips for integrating CSS3 into Android WebView

Currently, I am working on an Android application that utilizes HTML5. My goal is to implement element rotation using CSS3 transformations. However, I need to ensure that the app remains compatible with Android 2.3. What would be the most effective metho ...

Only in Firefox, there seems to be an issue with how the CSS

Using JavaScript, I have added two buttons to the left of the filter box: https://i.sstatic.net/yp8mg.png This is the HTML code for the buttons: https://i.sstatic.net/DgT27.png Here is the CSS style for the buttonfile: .buttonfile { text-indent: 0; ...

Guide to creating a column plot graph with axis labels

Can anyone help me with plotting a graph in C# between 2 axes like the example shown below? https://i.sstatic.net/iUite.png I have created this graph using the chart control in C# with Visual Studio: https://i.sstatic.net/dSgGy.png I am trying to plot ...

Expanding image in table data to full screen upon clicking using jQuery

I am trying to display an image in my table and then use jQuery to pop up a modal with the full-screen image. However, the image only pops up at the top of the table data. This is the code I have so far: <tbody> <tr> <td><?php ...

Want to learn how to display a description below a photo when you hover over it in a photo gallery?

I am currently creating a basic photo gallery where hovering over a thumbnail displays an enlarged photo below it. My goal is to also have text displayed inside a white text box when hovering over each thumbnail, with unique descriptions for each. I need ...

Centralized navigation bar

I am currently facing a challenge with centering my nav bar and I'm not sure how to proceed. The nav bar is placed within a class and a div, which is causing confusion for me. I have attempted to align it to the center and also tried using float:cente ...

What exactly is the significance of "utilizing a universal class name"?

In my current project, I am utilizing Material-UI version 3.1.2 to create the interface. Previously, when I was using Bootstrap4, I included style="overflow-y: scroll; height: 100%" in my head tag to ensure a scrollbar is always present, preventing the ap ...

How can one element be made to rely on the presence of another element?

I'm currently working on my portfolio website and encountering an issue. Whenever I hover over an image of a project, the image expands in size, becomes less transparent, and a description of the project pops up. However, I would like to include a lin ...

Tips for changing the width of a child element in CSS without impacting the parent's width

Here is the scenario: <div class="parent"> <div class="sizer" /> <div class="child" /> </div> .sizer { width: 200px; } .child { position: relative; width: 400px; } I am facing restrictions - I cannot explicitly set the width ...

Applying scoped CSS styles to parent elements in HTML5

I've been delving into the details of the HTML5 specification, specifically regarding the scoped attribute on style elements. According to the specification: The scoped attribute is a boolean attribute. When used, it signifies that the styles are mean ...

Setting the z-index to place an absolutely positioned element below a relatively positioned one

I am currently facing a challenge where I need to position an element under another one that has already been relatively positioned. The blue box must remain relatively positioned due to specific constraints within the website development process. For bet ...

Unable to adjust iframe height

Below is a snippet of code that I am working with: <style type="text/css"> .div_class { position:absolute; border:1px solid blue; left:50%; margin-left:-375px; margin-top:100px; height:300px; width:500px; overflow:hidden; } .iframe_class { position: ...

Liquid backdrop centered immobile elements

I'm facing some challenges trying to figure out the most efficient and correct way to achieve this layout, similar to Stack Overflow's design. I want a header with a background color that spans the entire width of the page, while keeping the cont ...

Performing a task through an AJAX call in DNN MVC

During my DNN MVC development journey, I encountered another issue. I am unsure if this is a bug, a missing feature, or a mistake on my part. Let me elaborate on the problem below. My Objective I aim to trigger an action in my controller by making an AJA ...

Ways to enlarge a div and its contents when hovering over it?

I am struggling to create a div that expands on hover and have been unable to make the content expand along with the div. I've experimented with various overflow options, but none of them seem to work. .grow { padding: 5px 5px 5px 5px ...

On iPhones, the links display as oversized blank containers

Recently, a client who switched from an Android phone to an iPhone reached out to me with a complaint. She mentioned that the links on her website, which I built for her, appear as large empty boxes. You can view her website here. To illustrate the issue ...

Ways to ensure CSS affects icon inside main element?

Struggling to match the background color of Material-UI icons with the rest of the list items on hover. The CSS is not applying to both the icon and text despite styling the overall class (className= "dd-content-item"). Any assistance would be greatly appr ...

The hamburger menu in Bootstrap collapses along with the navigation items when viewed on a mobile device

How can I resolve the issue of the hamburger menu icon collapsing with the nav-items when clicked? Navbar <header> <div class="container-fluid"> <div class="row"> <div class="col-12 col-sm-12"> ...

Troubleshooting issue with Selenium Webdriver C# where Firefox dropdown menu text remains unselected

In my current project, I am facing an issue while trying to select a text from the Dropdown Menu using Selenium webdriver in C#. The selection process works perfectly fine with Chrome browser, however, it does not seem to work with Firefox. Can anyone prov ...