Sitecore: Turning on HTML Caching blocks postback behavior

北战南征 提交于 2019-12-01 19:04:10

Enabling caching for a sublayout means you are bypassing the code entirely and Sitecore is just serving up the same HTML it generated previously. So it's behaving as designed. In other words, this does not seem to be a scenario where you can take advantage of sublayout caching.

As posted previously, this is expected behavior precisely because the page is being fetched from the cache. You can still support caching for non-postback loads, but the easiest way I've found is to sense the postback with code in Global.asax and switch accordingly, as in the sample below.



    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (context.Request.RequestType.Equals("POST"))
        {
            context.Response.Cache.SetNoServerCaching();
            return "POST " + DateTime.Now.Ticks + " " + context.Request.RawUrl;
        }

        switch (custom)
        {
            case "RAWURL":
                return context.Request.RawUrl;
            default:
                return "";
        }
    }

Then you can hook this to output cache directives in your controls:

<%@outputcache duration="3600" varybyparam="none" varybycustom="RAWURL" %>

Note that if you do it this way, you lose the easy ability to vary by a control's data source.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!