简体   繁体   English

关于ASP.NET Cache类的问题

[英]Question about ASP.NET Cache class

I would like to use Cache for my web application. 我想将Cache用于我的Web应用程序。

Here is the question 这是个问题

I found this link, http://msdn.microsoft.com/en-us/library/18c1wd61.aspx 我找到了这个链接, http://msdn.microsoft.com/en-us/library/18c1wd61.aspx

For that link, all the example use something like Cache["KeyName"]="blah blah blah" ; 对于该链接,所有示例都使用类似Cache["KeyName"]="blah blah blah" ;

When I try to do the same thing, I have an error message, said "using System.Web.Caching.Cache is a type but used like a variable" 当我尝试做同样的事情时,我有一条错误消息,说"using System.Web.Caching.Cache is a type but used like a variable"

What should I do? 我该怎么办?

Do I have to create an instance? 我是否必须创建实例?

My Example 我的例子

string test = "123";
  if (HttpContext.Cache["test"] != null)
    test = (string)HttpContext.Cache["test"];
else
    HttpContext.Cache["test"] = test;

I think you are getting some overlap of naming. 我认为你的命名有些重叠。 Be explicit and see if it works: 要明确,看看它是否有效:

HttpContext.Current.Cache["KeyName"]="blah blah blah";

You can also do the following in your ASP.NET codebehind: 您还可以在ASP.NET代码隐藏中执行以下操作:

Page.Cache["KeyName"]="blah blah blah";

or 要么

this.Cache["KeyName"]="blah blah blah";

Cache is handled by ASP.NET so you just have to use it, not create it. Cache由ASP.NET处理,因此您只需使用它,而不是创建它。

EDIT: In ASP.NET MVC you can use the following in your controller: 编辑:在ASP.NET MVC中,您可以在控制器中使用以下内容:

HttpContext.Cache["KeyName"]="blah blah blah";

You don't have to create an instance. 您不必创建实例。 ASP.NET does it for you automatically. ASP.NET会自动为您完成。

Use the HttpContext.Current.Cache . 使用HttpContext.Current.Cache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM