2010-01-06 5 views
3

나는 다음과 같은 조치가 :ASP.NET MVC OutputCacheAttribute : 매개 변수가 설정된 경우 캐시하지 않습니까?

public class HomeController : Controller 
{ 
    public ActionResult Index(int? id) { /* ... */ } 
} 

내가 해당 작업을 [OutputCache]하고 싶습니다,하지만 난 그 싶습니다을 중 하나는 캐시를 사용하지 않는

  • id == null 경우; 또는
  • 캐시가 다른 경우에는 id == null 인 경우 캐시를 사용합니다.

나는 내가 이것을 달성 할 수 있다고 생각 :

public class HomeController : Controller 
{ 
    [OutputCache(VaryByParam = "none", Duration = 3600)] 
    public ActionResult Index() { /* ... */ } 

    [OutputCache(VaryByParam = "id", Duration = 60)] 
    public ActionResult Index(int id) { /* ... */ } 
} 

그러나이 솔루션은 id 실제로 선택 사항이 작업을 의미한다, 그래서 이것은 몇 가지 코드의 반복을 만들 수 있습니다. 물론 나는 비슷한 것을 할 수있다.

public class HomeController : Controller 
{ 
    [OutputCache(VaryByParam = "none", Duration = 3600)] 
    public ActionResult Index() { return IndexHelper(null); } 

    [OutputCache(VaryByParam = "id", Duration = 60)] 
    public ActionResult Index(int id) { return IndexHelper(id); } 

    private ActionResult IndexHelper(int? id) { /* ... */ } 
} 

그러나 이것은 추한 것처럼 보인다.

어떻게 구현하나요?

+1

확실하지, 그래서 하나. – Gregory

답변

3

나는 아마도 당신이 가지고있는 것이 가장 깨끗한 옵션이라고 생각한다.

테스트하지 않은 다른 옵션은 VaryByCustom 매개 변수를 설정하고 Global.asax에서 GetVaryByCustomString을 무시하는 것일 수 있습니다.

public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
    if (arg.ToLower() == “id”) 
    { 
     // Extract and return value of id from query string, if present. 
    } 

    return base.GetVaryByCustomString(context, arg); 
} 

자세한 내용은 여기를 참조하십시오 : http://codebetter.com/blogs/darrell.norton/archive/2004/05/04/12724.aspx이을 downvoted 이유

관련 문제