2012-10-17 8 views
6

आउटपुट कैश को नीचे कोड का उपयोग करके एएसपी.नेट एमवीसी 2 में लागू नहीं किया जाता है।क्यों GetVaryByCustomString को

GetVaryByCustomString विधि को नहीं कहा जाता है: अपनी पहली पंक्ति में ब्रेकपॉइंट लगाकर और चलने वाले एप्लिकेशन से पता चलता है कि ब्रेकपॉइंट नहीं पहुंचा है। नियंत्रक सूचकांक() में ब्रेकपॉइंट तक पहुंच गया है।

एएसपी.नेट एमवीसी 2 में VaryByCustom का उपयोग कैसे करें?

नियंत्रक:

 [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 परिभाषा गलत है। आप निर्दिष्ट करना होगा Duration:

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

अब आप अपने अधिरोहित GetVaryByCustomString विधि बुलाया जाएगा। यह भी न भूलें कि GetVaryByCustomString विधि को नियंत्रक कार्रवाई निष्पादित करने के बाद ही कॉल किया जाएगा।

+0

धन्यवाद। इस वेरी को जोड़ने के बाद: * हेडर ब्राउज़र पर भेजा जाता है और यह ब्राउज़र कैश को अक्षम करता है। VaryByCustom के साथ ब्राउज़र कैश को कैसे सक्षम करें? – Andrus

1

मैं सिर्फ 2 अन्य कारणों

उल्लेख करने के लिए अगर वहाँ है किसी भी [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

एक परियोजना है कि मैं हाल ही में पर काम किया एक वैश्विक फिल्टर काम करने से उत्पादन कैशिंग को रोकने के लिए किया था।

संबंधित मुद्दे