How not to cache an ASP.NET user control?

I use OutputCache on my page with a user control, but I don’t want to cache this particular user control because it is associated with user input (if I access the page, I see the page as if I were authenticated by another user).

How can i do this?

+5
source share
3 answers

Personally, I use the VaryByCustom attribute so that you can log in and out with different types of cached pages:

<%@ 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'm just going to throw it away. What about substitution control?

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

- msdn:

Substitution , , .... , . - , , , .

, , , - .

+10

, , , . , . .

VaryByHeader="Cookie" cookie, cookie. VaryByCustom="SomeString" SomeString, GetVaryByCustomString Global.asax.

+1

You can create a cache filter: http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx

Check this filter if the user is registered or not.

0
source

All Articles