0

I've got a webAPI that uses Entity Framework. I'm trying to cache some data in the session variable following along in this article:

https://msdn.microsoft.com/en-us/library/system.web.httpcontext.session(v=vs.110).aspx

I can't seem to do it though. The Session object isn't available.

In my controller, I try this:

Session["mappings"] = mappings;

...but it doesn't recognize what Session is.

I also try this:

HttpContext.Current.Session["mappings"] = mappings;

...and this:

Page.Session["mappings"] = mappings;

...but it doesn't know what HttpContext or Page are.

I'm including System.Web in my project references. I'm also including this in my web.config:

...just like this article says:

https://msdn.microsoft.com/en-us/library/ms178581(v=vs.110).aspx

...but to no avail.

My work colleague suggests it's because our webAPI is RESTful which means it's stateless, so no session object. However, we know there are ways around this. What I need is simply some way of persisting data in some kind of cache that will survive across several requests.

I also need something that will be available inside EF entities (not just the webAPI controller) is that's possible.

Does anyone know how to solve this problem? Thanks.

gib65
  • 1,709
  • 3
  • 24
  • 58

1 Answers1

0

As your colleagues correctly suggested, an API is stateless, each request is separate and needs to have all the data required to complete the request.

You can add a caching layer however, but that is not going to be done via the Session object. Session makes no sense in an API.

Have a look here for some ideas: Caching Data in Web API

Andrei Dragotoniu
  • 6,155
  • 3
  • 18
  • 32