Monday, 16 May 2016

Add Custom output caching on listing page in asp.net mvc



Add below attribute on controller's method.

[OutputCache(VaryByParam = "None", VaryByCustom = "Host", Duration = 14400)]
public ActionResult Index(string category)

Add below method in gobal.asax file


        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom == "Host")
            {
                string sCategory = "";
             

                if (!string.IsNullOrEmpty(HttpUtility.ParseQueryString(context.Request.Url.ToString()).Get("CategoryName")))
                {
                    sCategory = HttpUtility.ParseQueryString(context.Request.Url.ToString()).Get("CategoryName");
                }
           

                return context.Request.Url.Host + context.Request.Url.Port + sCategory ;
            }
            return String.Empty;
        }

No comments:

Post a Comment