1
我覺得我要麼離得很近,要麼就在幾英里之外--MVC/Razor還沒有在我的駕駛室中。我在SO和其他地方看過太多「動態CSS」鏈接列出。將動態CSS添加到MVC中的靜態CSS文件內容
我有一個靜態CSS文件(〜/ Content/site.css),我想添加額外的CSS類(在我的情況下,基於數據庫中的數據)。
我創建了以下內容:
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");
}
}
在我_layout文件:
<head>
<link href="@Url.Action("DynamicCss", "CssController")"
rel="stylesheet"
type="text/css" />
</head>
我想我想知道我的錯誤是在這段代碼是什麼,但如果有另一個「最佳實踐」,你可以指向我,我會很感激。