Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How not cache an ASP.NET user control?

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, I see the page as if I were authenticated with another user).

How can I do that?

like image 952
gsb Avatar asked Aug 10 '09 18:08

gsb


People also ask

How do I disable cache in web config?

If you want to disable caching of all files in a folder and its subfolders you can add a Web. config in the root folder you want to disable browser caching for. Say you wanted to disable browser caching for the files in the folder js/nocache and all subfolders, then you would place the Web. config here: js/nocache/Web.

How caching is done in ASP.NET explain with example?

To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web. config file.

Where is .NET cache stored?

The cached data is stored in server memory. Class Caching : Web pages or web services are compiled into a page class in the assembly, when run for the first time. Then the assembly is cached in the server.

What are the different caching techniques available in .NET MVC?

Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser. Server- Content is cached on the Web Server. ServerAndClient- Content is cached on the Web Server and the Web Browser.


2 Answers

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.

like image 146
Chris Mullins Avatar answered Sep 30 '22 01:09

Chris Mullins


You can cache a page and you can cache a user control, but you can't cache a page except for a user control. When the user control runs the entire page has to run. You have to make the output cache for the page recognise the different users.

You can use VaryByHeader="Cookie" to cache the page for each set of cookies if the user identity is stored in a cookie. You can use VaryByCustom="SomeString" and implement a check for SomeString to do your own check for user identity in the GetVaryByCustomString method in Global.asax.

like image 35
Guffa Avatar answered Sep 29 '22 23:09

Guffa