Reputation: 2212
I am trying to access the Session in Controllers which are part of a webform project. All the session variables are set on ASPX pages
Session["SESSION_KEY_UI_CULTURE"] = ddlLanguage.SelectedValue;
Now on API request from client i need to read Session Variables and perform a task but Session is always null.
[RoutePrefix("api/accounts")]
public class AccountsController : ApiController
{
[Route("config")]
[HttpGet]
public IHttpActionResult GetCompanyConfig()
{
if(HttpContext.Current.Session != null)
{
//Session is always NULL
}
return Ok();
}
}
I have tried removing and adding SessionStateModule in WebConfig file
<remove name="Session" />
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
Upvotes: 2
Views: 939
Reputation: 671
ASP.NET Web Api is meant for REST Services which are by definition stateless. Adding the Session back into Web Api defeats the purpose of having a RESTful API. You may want to reconsider what you're trying to achieve and find another way to solve your Problem.
But there is a way to do what you want:
To activate Session nontheless add the following to your Global.asax
C#
protected void Application_PostAuthorizeRequest() {
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
VB
Sub Application_PostAuthorizeRequest()
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required)
End Sub
Here and here are 2 Topics that are going into the same direction which you might want to look at for further information
Upvotes: 1