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

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

问题描述

例如,在一个ASP.NET页面,你会做这样的事情

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

Cache.Add({...})并通过访问其缓存[钥匙] 。在这种情况下,缓存是的System.Web.Caching.Cache 对象。

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

反正有做这种类型的ASP.NET应用程序级缓存的Web API控制器?

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中的名称是主机无关,ASP.NET/IIS不是唯一的宿主选项。它可以是自托管为好。东西给你才走这路线来考虑。

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

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

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