0

I have a controller and two actions in it:

public ActionResult Index()
{
    this.Session.Add("Boo", "Foo");
    return View();
}

public ActionResult Details()
{
    Debug.WriteLine(this.Session["Boo"]);
    return View();
}

Then on the Index View I have a link

<div class="form-group">
    @Html.ActionLink("Details", "details")
</div>

When Index action is invoked I set session variable "Boo", then in the Index View, I click on the Details link and when I get to Details action, Session ID is different from what it was in the Index action, and obviously this.Session["Boo"] is null.

Any ideas why is it happening?

Thanks!

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
shlasasha
  • 165
  • 1
  • 14

1 Answers1

0

This happens when cookies are disabled in a browser. Therefore, the session does not preserve the state.

Even with browsers that do support cookies, some users prefer to turn off cookie support. If your application needs to be responsive to browsers that don't support cookies, you cannot use ASP session management.

In this case, you must write your own mechanism to pass information from page to page in your application. There are two general ways to do this:

  • Add parameters to a URL's query string.
  • Add hidden values to a form.

For more information see documentation: Using Cookies to Maintain Sessions in ASP

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • Unfortunately no. Cookies are enabled. – shlasasha Nov 30 '20 at 00:00
  • @shlasasha: Try different advises listed in [ASP.NET: Session.SessionID changes between requests](https://stackoverflow.com/q/2874078/6630084). Very difficult to help you without [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Jackdaw Nov 30 '20 at 00:28
  • @shlasasha: For example, many users advice to initialize the Session object within Global.asax.cs: `void Session_Start(object sender, EventArgs e) { HttpContext.Current.Session.Add("__MyAppSession", string.Empty); }`. **Will find why the session is changing will get a solution!** – Jackdaw Nov 30 '20 at 00:32