Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to add sessionstate to my existing asp.net application. I have added
C#
httpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required)

to the global.asax file but each time i make a call to the api, a new session is created.

Here is how i am trying to view the session
C#
var session = HttpContext.Current.Session;
            if (session != null)
            {
                if (session["TestSession"] == null)
                {
                    session["TestSession"] = DateTime.Now;
                }
                return "TestSession: " + session["TestSession"];
            }

            return ("No Session");

Just wondering if anyone else has encountered this problem, i am using a session state sqldatabase to store the session

What I have tried:

i have tried adding httpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required) to the global.asax

and then loading a value from the session
Posted
Updated 28-Jun-16 1:45am
v2
Comments
ZurdoDev 28-Jun-16 7:48am    
Does your WebMethod have the EnableSession flag set on?

[WebMethod(EnableSession = true)]
public string DoSomething(string someValue)
{


}
donnie_regan 28-Jun-16 8:40am    
This is my webapi controller, i have tried setting enableSession to true here but it made no difference.

public string Index()
{

var session = HttpContext.Current.Session;

if (session != null)
{
if (session["TestSession"] == null)
{
session["TestSession"] = DateTime.Now;
}
return "TestSession: " + session["TestSession"];
}

return ("No Session");
}
ZurdoDev 28-Jun-16 8:53am    
See http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api
donnie_regan 29-Jun-16 5:41am    
Thanks i tried that but it is still creating a new session eachtime, it must a setting elsewhere within the existing application that is causing it.
Kornfeld Eliyahu Peter 28-Jun-16 7:48am    
Where in Global.asax that line is?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900