• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

HttpContext.Current.Items ["value"]没办法正常工作,因为AngularJS调用会创建新会话

用户头像
it1352
帮助1

问题说明

我正在使用C#,MVC和AngularJS.

I'm using C#, MVC and AngularJS.

我的问题是我的MVC程序创建了一个HttpContext.Current.Items["value"]并在初始home控制器中设置了值,但是当AngularJS用ajax调用命中应用程序时,它创建了一个新会话,但我无法获取该值我在HttpContext.Current.Items["value"]调用中进行了设置.

My problem is that my MVC program creates a HttpContext.Current.Items["value"] and sets the value in the initial home controller, but when my AngularJS hits the application with an ajax call, it creates a new session and I can't get the value I set earlier in my HttpContext.Current.Items["value"] call.

我可以做些什么来解决此问题吗?我想继续使用HttpContext.Current.Items["value"].

Is there something I can do to fix this problem? I'd like to continue using HttpContext.Current.Items["value"].

为什么我的AngularJS调用会创建新的sessionid?我知道会话是新会话的原因是因为当我使用此会话时,它们具有不同的ID:

Why do my AngularJS calls create new sessionids? The reason I know the sessions are new is because they have different ids when I use this:

String strSessionId = HttpContext.Session.SessionID;

正确答案

#1

HttpContext.Current.Items是仅用于请求缓存的字典.请求完成后,请求中的所有值都将超出范围.

HttpContext.Current.Items is a dictionary for request caching only. As soon as the request is finished all of the values in it will go out of scope.

// Will last until the end of the current request
HttpContext.Current.Items["key"] = value;

// When the request is finished, the value can no longer be retrieved
var value = HttpContext.Current.Items["key"];

HttpContext.Current.Session是一个字典,用于存储请求之间的数据.

HttpContext.Current.Session is a dictionary that stores data between requests.

// Will be stored until the user's session expires
HttpContext.Current.Session["key"] = value;

// You can retrieve the value again in the next request, 
// until the session times out.
var value = HttpContext.Current.Session["key"];

HttpRequest.Current.Items值再次不可用的原因是因为您将其设置为在家庭控制器中",这是与AJAX调用完全不同的请求.

The reason why your HttpRequest.Current.Items value is not available again is because you are setting it "in your home controller", which is a completely separate request from your AJAX call.

会话状态取决于cookie,因此,如果将同一cookie发送回服务器,则可以检索在那里存储的数据.幸运的是,如果您在同一域中,则 AJAX将自动将Cookie发送回服务器.

Session state depends on a cookie, so if the same cookie is sent back to the server, the data stored there can be retrieved. Fortunately, if you are on the same domain, AJAX will automatically send the cookie back to the server.

关于更改SessionID, ASP.NET不会为会话分配存储直到使用.因此,您需要在会话状态中显式存储内容才能真正开始会话.有关更多信息,请参见此MSDN文章.

As for the SessionID changing, ASP.NET does not allocate storage for session until it is used. So you need to explicitly store something in session state in order to actually start a session. See this MSDN article for more information.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhefbcgg
系列文章
更多 icon
同类精品
更多 icon
继续加载