0

I am trying to consume a Web API from my code behind page and after getting the response from the Web Api, I always lose my Session values.

Dim formatter As New JsonMediaTypeFormatter()
    Using client = New HttpClient()
  Dim chkValue =  HttpContext.Current.Session("UserName") // Has a value here
        client.BaseAddress = New Uri(apiAddress)
        client.DefaultRequestHeaders.Accept.Clear()
        client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
        Dim response As HttpResponseMessage = Await client.PostAsync("requestURI", "customObject", "formatter")
        If response.IsSuccessStatusCode Then
                HttpContext.Current.Session("UserName") // Referenced object has value of nothing
        Else

        End If
    End Using

This is within a method using Async modifier, not sure if that could lead to something like this.

devSuper
  • 295
  • 1
  • 4
  • 18
  • You're using 2 different session objects to describe the problem. Are you saying that testing `HttpContext.Current.Session("UserName")` at the LoggedClientName line is where the reference exists vs later in the method where it does not? – Joel Etherton Feb 24 '15 at 17:57
  • Sorry it should have being the same. Edited now. – devSuper Feb 24 '15 at 17:59
  • I wouldn't say yours is a direct duplicate, but perhaps the answer to this related question may help: http://stackoverflow.com/questions/27672598/windows-phone-8-1-httpclient-and-session-cookies – Joel Etherton Feb 24 '15 at 18:02
  • Thanks for the link. I have tried it, but that did not work : ( – devSuper Feb 24 '15 at 18:09

1 Answers1

0

You may look at ASP.NET Web API session or something?: As of the first response, webapi is stateless, this link explains why.

If you need to store information, you may need store them in a cookie (not recommended) or store them in the application cache with a cookie value as the key.

Community
  • 1
  • 1
Stephen Reindl
  • 5,659
  • 2
  • 34
  • 38
  • I am only trying to consume a Web Api. So I don't understand why the session values set in my web forms are lost. – devSuper Mar 17 '15 at 12:41