C# 有没有办法从静态方法访问缓存或会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/385345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:27:43  来源:igfitidea点击:

Is there a way to access a cache or session from a static method?

c#asp.netjqueryajaxdynamic

提问by theo

How would you access the cache from a jQuery ajax call?

您将如何从 jQuery ajax 调用访问缓存?

I'm using jquery to do some data verification and quick data access. I have a static web-method that jquery is calling via json to return a value. I don't want to call to the database everytime so I'd like to cache the data I'm hitting, but I can't determine how to call the asp.net cache from within javascript, or a static method.

我正在使用 jquery 进行一些数据验证和快速数据访问。我有一个静态 web 方法,jquery 通过 json 调用它来返回一个值。我不想每次都调用数据库,所以我想缓存我正在访问的数据,但我无法确定如何从 javascript 或静态方法中调用 asp.net 缓存。

I'd like to send the page object through to the static method, which would allow me to access page.cache, but don't know how. Barring that, maybe a way to access the cache from javascript itself?

我想将页面对象发送到静态方法,这将允许我访问 page.cache,但不知道如何。除此之外,也许是一种从javascript本身访问缓存的方法?

采纳答案by Mark Brackett

System.Web.HttpContext.Current.Cache

System.Web.HttpContext.Current.Cache

Cache is shared per app domain - not per Page. Page just has a convenience property of Page.Cacheto get the current Cache, which means you can just do Cache["key"] from a method in a page.

缓存按应用程序域共享,而不是按页面共享。Page 只是有一个方便的 Page.Cache 属性来获取当前的 Cache,这意味着您可以从页面中的方法中执行 Cache["key"] 。

As you've noticed, if you're in a static method - then you have no Page instance, and you have no Page.Cache property. So, you need to use HttpContext.Cache. But, wait - you have no HttpContext instance either! That's ok, the currently executing instance is stored at the static property of HttpContext.Current.

正如您所注意到的,如果您使用的是静态方法 - 那么您就没有 Page 实例,也没有 Page.Cache 属性。所以,你需要使用HttpContext.Cache。但是,等等 - 你也没有 HttpContext 实例!没关系,当前正在执行的实例存储在HttpContext.Current的静态属性中。

So - to answer your question - in a static method, use HttpContext.Current.Cache. BTW, you can also access the Requestand Responseproperties from there.

所以 - 回答你的问题 - 在静态方法中,使用 HttpContext.Current.Cache。顺便说一句,您还可以从那里访问请求响应属性。

回答by Filip Ekberg

Javascript is client side, the Cache is on the Server Side, so you need to do a callback to a method in your asp.net application, that returns the content of the cache.

Javascript 是客户端,缓存在服务器端,因此您需要对 asp.net 应用程序中的方法进行回调,该方法返回缓存的内容。

The ASP.NET Cache API is really good, you can use Cache["Key"] to get the cached content that you like. Read more here : http://msdn.microsoft.com/en-us/library/ms972379.aspx

ASP.NET Cache API 真的很好,你可以使用 Cache["Key"] 来获取你喜欢的缓存内容。在此处阅读更多信息:http: //msdn.microsoft.com/en-us/library/ms972379.aspx

回答by BobbyShaftoe

I think calling a PageMethod may be the best you can really do, if you really want to do this:

我认为调用 PageMethod 可能是你真正能做的最好的事情,如果你真的想这样做:

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/