Odd Session Behavior in ASP.NET

瘦欲@ 提交于 2019-12-13 03:03:56

问题


First off, this is my own project, not a homework assignment.

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:

Tab A is open to Page A

Tab B is open to Page B

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.

If I then refresh Tab B, then go back to Tab A and refresh, then the data in the session updates for Tab A.

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.

I'm not sure why the session data won't update until I refresh Tab B.


回答1:


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.



来源:https://stackoverflow.com/questions/26807706/odd-session-behavior-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!