1

I am trying to store some value in session from Web API 2 although is is against REST basic principle. But session lost with each request. It is a web form + web API project.

Added below code in Global.asax.cs

 protected void Application_PostAuthorizeRequest()
        {
            System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
        }

Adding session data in one controller as below.

HttpContext.Current.Session["ABC"] = "text";

With each request/api call, session lost.

I need to set the session in Web API and access it in Web Form.

Tried this answer, but getting error like "Error 2 Cannot implicitly convert type 'void' to 'System.EventHandler' "

user1926138
  • 1,464
  • 7
  • 34
  • 53
  • As an improvement, instead of using session, you may want to look at object caching using Memcache. You can select a unique key (depending upon the user primary key) for the caching that object and cache the data. Here is the nuget package for memcahe with dot net. https://www.nuget.org/packages/EnyimMemcached/ You can set the cache duration same as session timeout. – Himanshu Bhankar Jun 19 '17 at 10:17
  • Possible duplicate of [Accessing Session Using ASP.NET Web API](https://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api) – NightOwl888 Jun 19 '17 at 18:51

0 Answers0