开发者

ASP.net do not cache page with a specific querystring

开发者 https://www.devze.com 2022-12-19 23:12 出处:网络
I know that i can use the VaryByParam attribute, but what it does is not exactly what i am aiming for.

I know that i can use the VaryByParam attribute, but what it does is not exactly what i am aiming for.

What happens is that different versions of the page are cached based on the query string.

For开发者_高级运维 example Page.aspx?ID=Tom has its own cached version and ID=Arnold has another. Now what i want to do is not to cache Arnold at all. I only want to cache the page when the query string is Tom.

Any ideas?


For C#

    public static void cacheOutput(Page page, double minutes)
    {
        page.Response.Cache.SetExpires(DateTime.Now.AddMinutes(minutes));
        page.Response.Cache.SetCacheability(HttpCacheability.Server);
        page.Response.Cache.SetValidUntilExpires(true);
    }

It's better to take a double for minutes. Good solution! Thx!


Try to use VaryByCustom and override GetVaryByCustomString method in the Application class.


    <Extension()> _
    Public Sub CacheOutput(ByVal Page As Page, ByVal Minutes As Integer)
        Page.Response.Cache.SetExpires(Date.Now.AddMinutes(Minutes))
        Page.Response.Cache.SetCacheability(HttpCacheability.Server)
        Page.Response.Cache.SetValidUntilExpires(True)
    End Sub

This works. Note that any version of the page that has a querystring or a POST parameter will not be cached.

0

精彩评论

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