【问题标题】:Losing Session Values After Calling Web API调用 Web API 后丢失会话值
【发布时间】:2015-02-24 17:54:41
【问题描述】:

我正在尝试从我的页面代码中使用 Web API,并且在从 Web Api 获得响应后,我总是丢失我的 Session 值。

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 修饰符的方法中,不确定这是否会导致这样的事情。

【问题讨论】:

  • 您使用 2 个不同的会话对象来描述问题。您是说在 LoggedClientName 行测试 HttpContext.Current.Session("UserName") 是引用存在的地方,而稍后在方法中不存在引用?
  • 抱歉应该是一样的。现已编辑。
  • 我不会说你的是直接重复的,但也许这个相关问题的答案可能会有所帮助:stackoverflow.com/questions/27672598/…
  • 感谢您的链接。我试过了,但没有用:(

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


【解决方案1】:

你可以看看ASP.NET Web API session or something?:在第一个响应中,webapi 是无状态的,this link 解释了原因。

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

【讨论】:

  • 我只是想使用一个 Web Api。所以我不明白为什么我的网络表单中设置的会话值会丢失。
猜你喜欢
  • 2014-03-21
  • 2014-04-18
  • 2016-01-21
  • 1970-01-01
  • 2013-10-21
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多