How not cache an ASP.NET user control?

后端 未结 3 1968
無奈伤痛
無奈伤痛 2021-01-02 23:15

I\'m using OutputCache in my page that has a user control, but I don\'t want to cache this specific user control because it\'s related to a user login (if I access the page,

3条回答
  •  难免孤独
    2021-01-03 00:15

    Personally I use the VaryByCustom attribute to give logged in and logged out users different cached page views:

    <%@ OutputCache VaryByCustom="IsLoggedIn" Duration="30" VaryByParam="*" %>
    

    then in global.asax you put

    public override string GetVaryByCustomString(HttpContext context,
        string arg)
    {
        if (arg == "IsLoggedIn")
        {
    
            if (context.Request.IsAuthenticated)
            {
                return "Logged in: " + context.User.Identity.Name;
            }
            else
            {
                return "Not Logged In";
            }
    
        }
        else
        {
            return base.GetVaryByCustomString(context, arg);
        }
    
    }
    

    I am just going to throw this out there. How about the substitution control?

    http://msdn.microsoft.com/en-us/library/ms228212.aspx

    According to msdn website:

    The Substitution control lets you create areas on the page that can be updated dynamically and then integrated into a cached page. ... The Substitution control offers a simplified solution to partial page caching for pages where the majority of the content is cached. You can output-cache the entire page, and then use Substitution controls to specify the parts of the page that are exempt from caching.

    I have never used the substituion control personally, but I just happened to look it up the other day, and it sounded like it can somehow inject updated content into an otherwise cached page output.

提交回复
热议问题