views:

225

answers:

2

Hi,

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 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,

A: 

Are all your AJAX calls using the same server name:

http://localhost/whatever

vs

http://machinename/whatever

rick schott
Both are to http://localhost:port/
madcapnmckay
+1  A: 

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 :)

rajesh pillai
That's interesting. However I was storing something in the session and then on a subsequent request was trying to retrieve it. I have added the Session_Start method and it seems to do the trick. Very confused.
madcapnmckay