Laura Cîrstea
Laura Cîrstea

Reputation: 147

C# the name 'Cache' does not exist in the current context

I'm trying to store some values in the Cache to use them later and I think that this is what I need https://msdn.microsoft.com/en-us/library/18c1wd61.aspx, but can't get it to work...

This is how I try to use it (added using System.Web) :

Cache["name"] = "my name";

And it says:

The name 'Cache' does not exist in the current context

What am I missing?

Upvotes: 1

Views: 2577

Answers (1)

Remus Rusanu
Remus Rusanu

Reputation: 294407

From Cache Class:

The instance of Cache that is accessed by the snippet below is a member of the Page object that this sample inherits.

Your code must be in a Page method. You have also another instance available on the HttpContext, again, described on the same link:

One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.

You have to consider that Cache["<somestring>"] it must be an instance, not the class, since a class cannot have a static index accessor. So any sample you see like that, it must be a an instance, likely a property named Cache of some object.

Upvotes: 1

Related Questions