开发者

Session Id Changing Randomly

开发者 https://www.devze.com 2022-12-08 03:01 出处:网络
I have a simple ASP.NET MVC application. When the first action method is run it stores some data in the Session variable. On the resulting view I have a jquery ajax call triggered by a button to anoth

I have a simple ASP.NET MVC application. When the first action method is run it stores some data in the Session variable. On the resulting view I have a jquery ajax call triggered by a button to another action method.

When I开发者_JS百科 click the button a different session id is used at the server side, it's a bit random. There is sometimes a gap of a second or so between starting and clicking the button and the Session ID still changes. This breaks the app as it tries to retrieve the data stored by the first action method.

Any idea what's going on? Both requests are to the same URL.

  1. I see method one instantiate a new session with Id X and store the data.
  2. Immediately after loading the Jquery request fires. I see a different session cookie id on the request header.
  3. I get an error "data not found"

Many thanks,


This is by design and ASP.NET tries to be efficient in storing sessions for users. Remember unless you store anything in session the session value changes.

If you want to tell ASP.NET that you want it to track user sessions, you can do one of 2 things:

  1. Store something in the session.
  2. Simple handle the Session_Start event in your GLobal.asax. The presence of this method will tell ASP.NET to track sessions , even if there is no data in the session..

// NOTE: There is no need to add any thing to session if you are doing this... public void Session_Start(object sender, EventArgs e)
{
}

This behavior had caused me much worry in the past :)


Are all your AJAX calls using the same server name:

http://localhost/whatever

vs

http://machinename/whatever

0

精彩评论

暂无评论...
验证码 换一张
取 消