BIG update after some good testing
I have a MVC4 web application and I'm simply debugging it in visual studio 2010 just to learn some more about webdevelopment (and MVC in particular). I'm playing with Session now. But I don't understand why I lose my variables after a new httprequest.
It's like this question: Losing my session variables
My web.config for the session part looks like:
<sessionState mode="InProc"
cookieless="false"
timeout="20"/>
My little test project to isolate the problem works fine and looks like:
Controllers - HomeController
public class HomeController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
string t = (string)Session["Test1"];
ViewBag.Result = t;
return View();
}
}
MysessionController
public class MysessionController : Controller { // // GET: /Mysession/
public ActionResult Index() { return View(new Models.Mysession() {ID = Session.SessionID}); } [HttpPost] public ActionResult Index(Models.Mysession mySession) { Session["Test1"] = "Bla"; return RedirectToAction("Index", "Home"); } }
Models
public class Mysession {
[Required]
public string ID { get; set; }
}
Views
Home
Index.cshtml
@{ ViewBag.Title = "Index"; }
Index
@@ViewBag.Result@ @((string)Session["Test1"]) @Html.ActionLink("My session", "Index", "Mysession")
Mysession
Index.cshtml
@model SessionTest.Models.Mysession @{ ViewBag.Title = "Index"; }
Index
@using (Html.BeginForm()) {
@Html.ValidationSummary(false) <fieldset> <legend>Mysession</legend> <div class="editor-label"> @Html.LabelFor(model => model.ID) </div> <div class="editor-field"> @Html.EditorFor(model => model.ID) </div> <p> <input type="submit" value="Log in" /> </p> </fieldset>
}
This looks in a nutshell like my main application, the DIFFERENCE is: In MysessionController in the HTTPPOST Index action where the session variable is set I acces a XML file to look up something.
The XML file is my persistent storage because I don't have a SQL server. My question is, can this affect my session?
If so, I would like to point out that I acces the XML file PRIOR to setting the variable in the session. So it seems strange to me. But I've pinpointed by testing that if I don't acces the XML file then the vars in the session are fine. If I acces the XML file the vars in the session are null after the RedirectToAction.
I don't know why I didn't came to this conclusion yesterday. Sorry everybody. I did some sloppy testing yesterday.