views:

111

answers:

1

I am doing a jquery $.post('/Home/jQueryRateForumItem', but when I get to my controller the session id is always different on each post.

I have tried;

        string g = HttpContext.Session.SessionID;
        string session = base.Session.SessionID;

I'm pretty sure The session id shouldn't change but can't for the life of me think how to fix this.

EDIT

It's happening when I refresh the Index page as weel so not just on ajax postbacks. I am using cassini and not IIS though this is the first time I've had issues

+2  A: 

I can't see anything wrong with your code and I couldn't reproduce the behavior you are describing. By default sessions are tracked by cookies so make sure that you've enabled cookies on your browser or it might create a new session on each request.

Also make sure you store a value into the session or the session cookie won't be created. Let's take for example the following controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = base.Session.SessionID;
        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

First navigate to the Home action, then go to About and when you come back to Home the session id changes. That's normal as nothing was stored into the session. If you simply set some value Session["foo"] = "bar"; it will work as the session cookie will be set by the server.

Darin Dimitrov
@Darin, that was it. I wasn't storing a value in the seesion beforehand. I handn't quite gotten to that stage. :) Thanks
griegs