2017-10-10 4 views
0

debug="true"に設定されている場合、OutputCache機能を無効にします。ASP.NET MVCのアプリケーション開始時にOutputCacheをプログラムで無効にする方法

私は正常に私のGlobal.asaxのApplication_Start()でこのメソッドを呼び出すことで、この値にアクセスすることができます。

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
{ 
    filters.Add(new HandleErrorAttribute()); 
    CompilationSection configSection = (CompilationSection)ConfigurationManager.GetSection("system.web/compilation"); 
    if (configSection?.Debug == true) 
    { 
     filters.Add(new OutputCacheAttribute() 
     { 
      VaryByParam = "*", 
      Duration = 0, 
      NoStore = true 
     }); 
    } 
} 

問題はOutputCacheが明示的に設定している私のコントローラ内の任意のエンドポイントだったグローバルフィルタを使用することはありません、ですセットアップ。その「月」プロファイルは、私のweb.configファイルで定義されているのはここ

[OutputCache(CacheProfile = "Month")] 
[HttpGet] 
public ViewResult contact() 
{ 
    return View(); 
} 

は次のとおりです。

<system.web> 
    <caching> 
    <outputCacheSettings> 
     <outputCacheProfiles> 
     <add name="Month" duration="2592000" location="Any" varyByParam="*" /> 
     </outputCacheProfiles> 
    </outputCacheSettings> 
    </caching> 
</system.web> 

私が「月」のように、明示的に定義されOutputCacheプロファイルの使用を無効にできるようにする必要があり、私はデバッグモードになっています。これどうやってするの?

答えて

0

に応じた変換を実装する必要がありますすべてのキャッシングがデバッグ中に無効になります。しかし、それはプログラム的な解決策ではないので、コードですべてのことを行う方法を考え出しました。これは、高度なユースケースでも機能します。

まず、我々は、それが起動したときにアプリがデバッグモードであるか否かをキャプチャしたいです。私たちはグローバル変数にそれを保存し、速やかに処理します。

public static class GlobalVariables 
{ 
    public static bool IsDebuggingEnabled = false; 
} 

はその後Global.asaxのコードのApplication_Start方法では、グローバルプロパティに書き込みます。

protected void Application_Start() 
{ 
    SetGlobalVariables(); 
} 

private void SetGlobalVariables() 
{ 
    CompilationSection configSection = (CompilationSection)ConfigurationManager 
     .GetSection("system.web/compilation"); 
    if (configSection?.Debug == true) 
    { 
     GlobalVariables.IsDebuggingEnabled = true; 
    } 
} 

今、私たちはOutputCacheAttributeから継承しますキャッシング、に使用する当社独自のクラスを作成します。あなたは、キャッシングのためにあなたのコントローラのエンドポイントを飾るとき

public class DynamicOutputCacheAttribute : OutputCacheAttribute 
{ 
    public DynamicOutputCacheAttribute() 
    { 
     if (GlobalVariables.IsDebuggingEnabled) 
     { 
      this.VaryByParam = "*"; 
      this.Duration = 0; 
      this.NoStore = true; 
     } 
    } 
} 

は今、単に[OutputCache]のではなく、あなたの新しい属性を使用します。

// you can use CacheProfiles or manually pass in the arguments, it doesn't matter. 
// either way, no caching will take place if the app was launched with debugging 
[DynamicOutputCache(CacheProfile = "Month")] 
public ViewResult contact() 
{ 
    return View(); 
} 
1

あなたはこのようにプロフィールを宣言することができ、デバッグのみの構成のための特別なweb.configファイル、作成することができます。

<system.web> 
    <caching> 
    <outputCacheSettings> 
     <outputCacheProfiles> 
     <add name="Month" duration="0" location="Any" varyByParam="*" /> 
     </outputCacheProfiles> 
    </outputCacheSettings> 
    </caching> 
</system.web> 

をし、アプリケーションをデバッグ構成で構築されたときにはキャッシュを持っていません。

Transform config

Different configurations

今、あなたは、単に戻るの答えは、おそらく望むの標準的な使用のケースのために働く最もストレートフォワードソリューションです@article

関連する問題