是否可以在 Web API 中使用 ASP.NET 应用程序缓存? [英] Is it possible to use ASP.NET application caching in web API?

查看:22
本文介绍了是否可以在 Web API 中使用 ASP.NET 应用程序缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在 ASP.NET 页面中,您会执行类似的操作

For example, in a ASP.NET page you would do something like

Cache.Add({...}) 并通过 Cache["key"] 访问它.在这种情况下,Cache 是 System.Web.Caching.Cache 对象.

Cache.Add({...}) and access it via Cache["key"]. In this context, Cache is the System.Web.Caching.Cache object.

无论如何要在 Web API 控制器中进行这种类型的 ASP.NET 应用程序级缓存?

Is there anyway to do this type of ASP.NET application level caching in web API controllers?

推荐答案

如果您是虚拟主机,为什么不呢?

If you are web hosting, why not?

var context = HttpContext.Current;

if (context != null)
{
    if (context.Cache["g"] == null)
    {
        context.Cache["g"] = 9.81;
    }
}

但是您这样做是在添加对 ASP.NET 的依赖.尽管 ASP.NET Web API 在名称中包含 ASP.NET,但 Web API 与主机无关.也就是说,ASP.NET/IIS 不是唯一的托管选项;Web API 也可以是自托管的.在走那条路之前,您需要考虑一些事情.

But you are adding a dependency on ASP.NET by doing so. Even though ASP.NET Web API has ASP.NET in the name, the Web API is host-agnostic. That is, ASP.NET/IIS is not the only hosting option; the Web API can be self-hosted as well. Something for you to consider before going down that route.

这篇关于是否可以在 Web API 中使用 ASP.NET 应用程序缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆