2013-01-10 2 views
1

Json을 반환하는 동작의 OutputCache 특성이 작동하지 않습니다. 브라우저에서 동작 URL을 여러 번 누르는 경우 VS2012에서 활성화 된 중단 점을 얻을 때마다 OutputCache 특성이 무시되는 것처럼 보입니다). 여기 내 코드는 다음과 같습니다.JsonResult (asp.net-mvc-3)에 대해 Outputcache가 작동하지 않습니다.

public class ApiController : GenericControllerBase 
{ 

    [OutputCache(Duration = 300, VaryByParam = "type;showEmpty;sort;platform")] 
    public JsonResult GetCategories(string type, bool? showEmpty, string sort, string platform) 
    { 
     ///... creating categoryResults object 
     return Json(new ApiResult() { Result = categoryResults }, JsonRequestBehavior.AllowGet); 
    } 

} 

GenericControllerBase가 Controller를 상속합니다. GenericControllerBase에서 상속 한 다른 컨트롤러에서는 OutputCache가 예상대로 작동하지만 Json 대신 View()를 반환합니다. 실험으로 VaryByCustom 매개 변수를 추가하고 global.asax 파일의 GetVaryByCustomString 메서드가 손상되지 않았는지 확인하여 캐싱 기능이 완전히 무시되었습니다. 서비스 주입을 위해 MVC3과 autofac을 사용합니다 (그러나 Autofac 주입을 사용하는 다른 컨트롤러는 OutputCache와 제대로 작동합니다).

무엇이 문제 일 수 있습니까? OutputCache 기능을 차단할 수있는 기능은 무엇입니까? 전체 응답을 캐싱하는 것과 관련이있을 수 있습니까? 내 프로젝트에서 OutputCache를 사용하는 다른 모든 작업은 뷰에서 @ Html.Action (...)이 포함 된 부분 작업입니다.

MVC3의 GET 동작에서 전체 Json 응답을 캐시하는 가장 좋은 방법은 무엇입니까? 이 OutputCache를 완료 페이지를 반환 행동에 대한 무시 밝혀졌다 몇 가지 테스트 (뿐만 아니라 JSON) 후

업데이트

. 캐시는 프로젝트의 하위 작업에서만 작동합니다. 그 원인은 무엇일까요?

답변

4

결국 OutputCache 특성을 무시하고 MVC3에 대한 해결 방법을 사용했습니다. 나는 캐싱에 대한 사용자 정의 액션 필터를 사용 : 속성 위

public class ManualActionCacheAttribute : ActionFilterAttribute 
{ 
    public ManualActionCacheAttribute() 
    { 
    } 

    public int Duration { get; set; } 

    private string cachedKey = null; 

    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     string key = filterContext.HttpContext.Request.Url.PathAndQuery; 
     this.cachedKey = "CustomResultCache-" + key; 
     if (filterContext.HttpContext.Cache[this.cachedKey] != null) 
     { 
      filterContext.Result = (ActionResult)filterContext.HttpContext.Cache[this.cachedKey]; 
     } 
     else 
     { 
      base.OnActionExecuting(filterContext); 
     } 
    } 

    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     filterContext.HttpContext.Cache.Add(this.cachedKey, filterContext.Result, null, DateTime.Now.AddSeconds(Duration), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null); 
     base.OnActionExecuted(filterContext); 
    } 
} 

정확한 URL 경로 및 주어진 시간에 대한 쿼리를 기반으로 요청을 캐시하고 예상대로 작동합니다.

관련 문제