I know this is an old thread, I am fairly new to ASP.NET.
Working with ASP.NET Core, I am trying to use @Darin Dimitrov's answer to allow me to set custom styles using data from an external API. To the OP's question, I know you said "Dynamic" but it seems as though you really want "Compiled". I am looking for "Dynamic".
It appears the technique of returning a partial view with response type text/css works pretty well for this solution. However, the syntax used here does not work for me in ASP.NET Core 3.1. Here is the solution I used:
Instead of HomeController I am using ThemeController as it is relevant to me.
In ThemeController.cs
public async Task<IActionResult> Index()
{
var colors = await _apiService.GetThemeColors();
Response.ContentType = "text/css";
return PartialView(colors);
}
In the view ~/Views/Theme/Index.cshtml
@model ThemeColors
@{
var color = Model.primaryColor;
}
.btn-primary {
background-color: @color;
}
And then in my _Layout.cshtml
<link href="@Url.Action("Index", "Theme")" rel="stylesheet" type="text/css" />
I hope this helps someone, if anyone who is more knowledgeable around this topic can point to more relevant resources that would be greatly appreciated.