I feel like I'm either really close on this, or miles away - MVC/Razor isn't yet in my wheelhouse. I've looked at too many "Dynamic CSS" links in SO and elsewhere to list.
I have a static CSS file (~/Content/site.css) that I want to add additional CSS classes to (in my case, based on data from a database).
I created the following:
public class CssController : Controller
{
private string GetCustomCss()
{
var builder = new StringBuilder();
var colorInfo = mRepository.GetColors();
foreach (var detail in colorInfo.ResultValue)
{
builder.Append(detail.CustomName);
builder.Append("-light-color");
builder.Append(" { ");
builder.Append("color: ");
GetLightColor(detail, builder);
builder.Append("; ");
builder.Append(" } ");
}
}
return builder.ToString();
}
public ContentResult DynamicCss()
{
var siteCss = string.Format("{0}{1}",
System.IO.File.ReadAllText(Server.MapPath("/Content/Site.css")),
GetCustomCss());
return Content(siteCss, "text/css");
}
}
And in my _Layout file:
<head>
<link href="@Url.Action("DynamicCss", "CssController")"
rel="stylesheet"
type="text/css" />
</head>
I guess I'd like to know what my bug is with this code, but if there is another "Best Practice" that you could point me to, I'd appreciate it.