开发者

Changing OutputCache based on URL

开发者 https://www.devze.com 2022-12-12 15:18 出处:网络
I have got page which I would like to cache using the OutputCache directive. However, I am using a URL rewriter module to direct multiple URLs at this page, each with different contents.

I have got page which I would like to cache using the OutputCache directive. However, I am using a URL rewriter module to direct multiple URLs at this page, each with different contents.

Is there any way to use the cache the output for each URL? There are no ot开发者_运维技巧her criteria by which I need to vary the cache results.


In the end this was quite simple to fix.

  1. Add the following directive to the page that needs to be cached:

    < %@ outputcache duration="600" location="Downstream" varybyparam="none" varybycustom="RawURL" %>

  2. Add this method to the global.asax file

    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        switch (custom.ToUpper())
        {
            case "RAWURL":
                return context.Request.RawUrl;
    
            default:
                return "";
        }
    }
    


Programatically you can set Caching options using Response.Cache. You can switch on your querystring variable and depending on the case, set properties on Response.Cache appropriately.

MSDN on Cache object
Another helpful article from aspalliance.com

0

精彩评论

暂无评论...
验证码 换一张
取 消