在ASP.NET奇数会话行为 [英] Odd Session Behavior in ASP.NET
问题描述
首先,这是我自己的项目,而不是一个家庭作业。
First off, this is my own project, not a homework assignment.
这里的情况。我有一个Web应用程序(ASP.NET W / MVC 5),我使用会话来存储某些东西(例如用户信息)。现在,有两个标签开放,各页面的一部分。我们会打电话给他们一个标签和Tab B.下面的情况:
Here's the situation. I have a web application (ASP.NET w/ MVC 5) and I use the session to store certain things (e.g. user info). Now, there are two tabs open, each on one part of the page. We will call them Tab A and Tab B. Here's the scenario:
选项卡是开放的网页A
Tab A is open to Page A
标签B是开放的网页B
Tab B is open to Page B
在选项卡B,用户确实需要一个会话变量的更新操作。因此,我通过Ajax调用到一个控制器方法写入新的数据到会话变量。
In Tab B, the user does an action which requires the update of a session variable. Thus, I write the new data to the session variable via an Ajax call to a controller method.
如果我刷新选项卡中,但是,在会话变量中的数据不适合选项卡中进行更新。
If I refresh Tab A, however, the data in the session variable does not update for Tab A.
如果我再刷新选项卡B,然后再返回到标签A和刷新,然后在选项卡中的会话更新数据。
If I then refresh Tab B, then go back to Tab A and refresh, then the data in the session updates for Tab A.
另外奇怪的是,如果我刷新选项卡,但从来没有刷新选项卡B,然后按Tab乙认为会话数据永远不会更新无论是。
The other odd thing is, if I refresh Tab A, but never refresh Tab B, then Tab B thinks the session data never updated either.
我不知道为什么会话数据将不会更新,直到我刷新选项卡B点。
I'm not sure why the session data won't update until I refresh Tab B.
推荐答案
我见过这之前 - 这不是会话变量,当它发生在我身上,这是浏览器的缓存。我不得不设置页面不使用的服务器端和客户端的方法的组合缓存。
I've seen this before - it wasn't the session variable when it happened to me, it was the browser cache. I had to set the page not to cache using a combination of server-side and client-side methods.
客户端,我发现我需要做的这迫使所有我支持的浏览器不缓存:
Client side I found I needed to do this to force all my supported browsers not to cache:
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
服务器端我不得不这样做:
Server-side I had to do this:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "0");
一个有点蛮力,我承认,但是这是我可以让一切都在我的环境没有缓存的唯一方法。
A little brute force, I'll admit, but this was the only way I could make everything not cache in my environment.
这篇关于在ASP.NET奇数会话行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!