Is it possible to use ASP.NET application caching in web API?

后端 未结 4 1496
北荒
北荒 2021-01-19 23:15

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

Cache.Add({...}) and access it via Cache[\"key\"]. In this context, Cache is th

4条回答
  •  执念已碎
    2021-01-19 23:40

    If you are web hosting, why not?

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

    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.

提交回复
热议问题