2017-10-10 16 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 명시 적이었다 글로벌 필터를 사용하지 않습니다 설정 설정. 여기

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

는 "월"프로필이 내 Web.config의에 정의되어있는 곳입니다 :

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

내가 "월"과 같이 명시 적으로 정의 OutputCache를 프로파일의 사용을 무효화 할 수 있어야합니다, 내가 디버깅 모드에있을 때. 어떻게해야합니까?

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

그리고 응용 프로그램이 디버그 구성에 내장되는 경우가 더 캐시가 없습니다 :

답변

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 파일을 만들 수 있습니다. 위로의 대답은 원하는의 표준 사용의 경우에 작동 가장 솔직 솔루션은 아마 @

Different configurations

Transform config

지금 당신은 단지 article