これは、_Layoutページに1行追加することで実現できますが、コントローラからtext/cssとしてCSSファイルを取得することができます。
ですから、このような何かたとえば、そのID
とコントローラ上のメソッドを呼び出すことができ、現在のテナントIDは、フロントエンドで利用可能であると仮定すると:
@Styles.Render(string.Format("/CustomizationController/GetCssForTenant?tenantId={0}", loggedTeanant == null ? (int?) null : loggedTenant.Id))
そして今、カスタマイズコントローラを作成
public class CustomizationController : Controller
{
//this will cache cliente side the css file but if the duration expires
// or the tenantId changes it will be ask for the new file
[OutputCache(Duration = 43200, VaryByParam = "tenantId")]
public FileResult GetCssForTenant(int? tenantId)
{
var contentType = "text/css";
//if teanant id is null return empty css file
if(!tenantID.HasValue)
return new FileContentResult(new byte[0], contentType);
//load the real css file here <-
var result = ...
//---
//if having problems with the encoding use this ...
//System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
//var content = encoding.GetBytes(result);
//---
Response.ContentType = contentType;
return new FileContentResult(result, contentType);
//return new FileContentResult(content, contentType);
}
}
これは、あなたが必要とするものを達成するのに役立ちます。これは実現可能なスケッチであることに注意してください。あなたは_layout
@Styles.Render(string.Format("/CustomizationController/GetCssForTenant?tenantId={0}", 1))
を私の提案、実装の使用この
public class CustomizationController : Controller
{
//this will cache cliente side the css file but if the duration expires
// or the tenantId changes it will be ask for the new file
[OutputCache(Duration = 43200, VaryByParam = "tenantId")]
public FileResult GetCssForTenant(int? tenantId)
{
var contentType = "text/css";
//if teanant id is null return empty css file
if(!tenantID.HasValue)
return new FileContentResult(new byte[0], contentType);
//load the real css file here <-
var result = Environment.NewLine;
if(tenantID = 1)
result "body{ background-color: black !important;}"
else
result "body{ background-color: pink !important;}"
result += Environment.NewLine;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
var content = encoding.GetBytes(result);
Response.ContentType = contentType;
return new FileContentResult(result, contentType);
}
}
の迅速な試みを行い、変更したい場合は、編集が 今すぐページの背景色を変更する必要があり
1を送信すると黒になり、2を送信するとピンクになります。 また、同じIDで2回質問するとステータスが304になることがネットワークでわかります。これはファイルcoキャッシュからのmes。 このIDを変更すると、ステータスはキャッシュされていないレスポンス200になります。
nullを渡すと、cssファイルが空になってデフォルトのCSSにフォールバックされます。
これはかなりうまくいきましたが、結局私はいろいろな問題を一度に解決する別のソリューションを使いました。私はRazorViewEngineをオーバーライドし、自分でプラグインしました。このように、私は複数の面で絶対的な完全なコントロールを持っていました。しかし、まだ多くのおかげで。それは素晴らしい解決策です – user853710