HttpContext.Current.Items ["value"]无法正常工作,因为AngularJS调用会创建新会话 [英] HttpContext.Current.Items["value"] not working because AngularJS calls create new sessions

查看:47
本文介绍了HttpContext.Current.Items ["value"]无法正常工作,因为AngularJS调用会创建新会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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;

推荐答案

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.

这篇关于HttpContext.Current.Items ["value"]无法正常工作,因为AngularJS调用会创建新会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆