调用Web API后丢失会话值

时间:2015-02-24 17:54:42

标签: .net vb.net asp.net-web-api async-await session-variables

我正在尝试从我的代码页面后面使用Web API,在从Web Api获取响应后,我总是丢失会话值。

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

这是在使用Async修饰符的方法中,不确定是否会导致类似这样的事情。

1 个答案:

答案 0 :(得分:0)

您可以查看ASP.NET Web API session or something?:从第一个回复开始,webapi是无状态的,this link解释了原因。

如果您需要存储信息,可能需要将它们存储在cookie中(不推荐),或者将它们存储在应用程序缓存中,并以cookie值为关键字。