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"; }
@@ViewBag.Result@ @((string)Session["Test1"]) @Html.ActionLink("My session", "Index", "Mysession")
Mysession
Index.cshtml
@model SessionTest.Models.Mysession @{ ViewBag.Title = "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.
First off, I would like to thank everybody that has put time in this question. After I got myself together and finally conducted a proper test I pinpointed the problem to accessing a XML file on the hard drive. Don't know what I was messing about yesterday...
I found trough Google several posts and articles about losing your session after accessing files. Like this one:
Fantastic case study by Tessa Ferrandez. And to lesser extend: http://forums.asp.net/t/998370.aspx/1
And:
http://blogs.msdn.com/b/toddca/archive/2005/12/01/499144.aspx
Conclusion
When you access files in any way of your website the AppDomain will be refreshed and this will refresh your session.
Never put a file you need to access through your website in your 'bin' folder. I've moved my XML file to App_Data and the variables are persisted in the Session.
Summed up my answer on my blog: http://dannyvanderkraan.wordpress.com/2013/08/21/losing-session-variables-with-asp-net/
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments