I have a site built using MVC 2. My problem is that Response.End() causes session lost the first time session is accessed.
Test case 1:
-
Start the application.
-
Go to Home/X. An item is added to
sessionandResponse.End()is called. -
Open Home/X again and check
Session["X"](add a break point before the assignation line.) It returns null. Execute the rest of the action which assign “X” to session again. -
Reopen Home/X. This time
Session["X"]returns correct value.
Test case 2:
-
Keep the web application running. Close the browser and reopen to open a new session. Visit Home/X.
-
Result:
Session["X"]always has a value.
Could anyone please explain to me why this happens and how to solve it?
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public void X()
{
Session["X"] = "X";
Response.End();
}
}
Adding
to Global.asax helps fix the problem.