2016-03-22 15 views
1

私はホストベースのマルチテナントをサポートする必要のあるサイトで作業しています。私が持っている問題は、すべてのテナント(1,2,3)のサブフォルダがCSSフォルダにあることです。 tenant_Xフォルダ内のマルチテナントASP MVCサイトのパス設定を処理する方法

CSS 
    |_ tenant_1 
    |_ tenant_2 
    |_ tenant_3 
    |_ tenant (virtual) 

ごとに特定のテナントをstyplingために使用されるカスタムCSSファイルがあります。

私の考えは、テナントのフォルダにマッピングされる仮想ロケーション(テナント)を何らかの形で作成し、_Layoutにcoudeの追加ラインが1つしか必要ではないということでした。私はMVCで深遠ではないと私は知っている、私はカスタムルートで動作するようになると思う。 このアプローチのもう1つの理由は、テナントのユーザーは他のテナントがいることを認識できないということです。ユーザーに間違ったファイルを読み込ませる可能性を排除する必要があります。

これは正しいアプローチですか?あなたはもっと良い方法を提案できますか?

答えて

1

これは、_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にフォールバックされます。

+0

これはかなりうまくいきましたが、結局私はいろいろな問題を一度に解決する別のソリューションを使いました。私はRazorViewEngineをオーバーライドし、自分でプラグインしました。このように、私は複数の面で絶対的な完全なコントロールを持っていました。しかし、まだ多くのおかげで。それは素晴らしい解決策です – user853710

関連する問題