2012-10-17 4 views
6

출력 캐시는 ASP.NET MVC2에서 아래 코드를 사용하여 구현됩니다.GetVaryByCustomString이 호출되지 않는 이유

GetVaryByCustomString 메서드가 호출되지 않습니다. 첫 번째 줄에 중단 점을 배치하고 응용 프로그램을 실행하면 중단 점에 도달하지 않은 것으로 나타납니다. 컨트롤러 인덱스()의 중단 점에 도달했습니다.

VaryByCustom을 ASP.NET MVC2에서 사용하는 방법은 무엇입니까?

컨트롤러 :

 [OutputCache(VaryByCustom = "user")] 
     public ActionResult Index(string _entity, string id) 
     { 
... 

Global.asax.cs는 :

public class MvcApplication : System.Web.HttpApplication 
{ 
    public override string GetVaryByCustomString(HttpContext context, string arg) 
    { 
     if (arg == "user") 
     { 
      HttpCookie cookie = context.Request.Cookies["Company"]; 
      if (cookie != null) 
       return Thread.CurrentPrincipal.Identity.Name + "," + cookie.Value; 
      return Thread.CurrentPrincipal.Identity.Name; 
     } 
     return base.GetVaryByCustomString(context, arg); 
    } 

} 

답변

7

귀하의 OutputCache를 정의가 잘못되었습니다. 이제 오버라이드 (override) GetVaryByCustomString 방법은

[OutputCache(VaryByCustom = "user", Duration = 50)] 
public ActionResult Index(string _entity, string id) 

호출됩니다 : 당신은 Duration를 지정해야합니다. 또한 컨트롤러 동작이 완료된 후에 만 ​​GetVaryByCustomString 메서드가 호출된다는 사실을 잊지 마십시오.

+0

감사합니다. 이 Vary : * 헤더를 추가하면 브라우저에 * 헤더가 전송되고 브라우저 캐시가 비활성화됩니다. VaryByCustom으로 브라우저 캐시를 사용하는 방법은 무엇입니까? – Andrus

1

난 그냥 발생하지 않습니다 프로젝트의 모든 [NOCACHE] 속성, GetVaryByCustomString가있는 경우이 다른 원인

을 언급하고 싶다.

당신이 넣으면

Location = OutputCacheLocation.Client, 

GetVaryByCustomString가 발생하지 않습니다. 예상대로 출력 캐싱 작업을 시작, 필터를 추가하는 라인을 주석 후

public class FilterConfig 
{ 
    public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
    { 
     filters.Add(new NoCacheResponseAttribute()); 
    } 
} 

public class NoCacheResponseAttribute : BaseActionFilterAttribute 
{ 
    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     var response = filterContext.RequestContext.HttpContext.Response; 
     response.Cache.SetCacheability(HttpCacheability.NoCache); 
     response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); 
     response.Cache.SetNoStore(); 
    } 
} 

:

0

내가 최근에 작업 한 프로젝트는 작업에서 출력 캐싱을 방지하는 글로벌 필터를했다.

관련 문제