ASP.NET MVC Session variables become null through HTML.Action ChildAction

EZA

New Member
I'm developing a simple test website using ASP.NET MVC3 and Razor syntax. It has \[code\]_LayoutPage.cshtml\[/code\] as master template which uses \[code\]@HTML.Action\[/code\] to print user id at the top of the site for each page. I implemented a \[code\]childAction\[/code\] named \[code\]userInfo\[/code\] for this partial view which reads the user id from \[code\]HTTPContext.Session\[/code\] and prints it out. The child action is implemented in a controller called \[code\]CommonActionController\[/code\] derived from \[code\]Controller\[/code\]. In addition to user id it also reads two more variables from session and prints it.\[code\]public class CommonActionController: Controller{ [ChildActionOnly] public ActionResult userInfo() { if(HTTPContext.Session["x-user-id"] != null) { ViewBag.UserId = (string)(HTTPContext.Session["x-user-id"]); ViewBag.UserFirstName = (string)(HTTPContext.Session["x-user-first-name"]); ViewBag.UserLastName = (string)(HTTPContext.Session["x-user-last-name"]); ViewBag.UserLoggedinSince = (DateTime)(HTTPContext.Session["x-user-logon-timestamp"]).ToString("f"); } return PartialView(); }}\[/code\]My main page controller called \[code\]HomeController\[/code\] has the dashboard functionality implemented in \[code\]Dashboard\[/code\] action (currently it just prints the word "Dashboard"). In this controller I have overridden \[code\]Controller.OnActionExecuting()\[/code\] method which validates that the user id exists in session. It reads total three variables from session just like the aforementioned childAction.\[code\]public class HomeController: Controller{ public HomeController() { } protected override void OnActionExecuting(ActionExecutingContext ctx) { base.OnActionExecuting(ctx); if(HTTPContext.Session["x-user-id"] == null) ctx.Result = new RedirectResult("logon/userlogon"); if(HTTPContext.Session["x-user-logon-timestamp"] == null) ctx.Result = new RedirectResult("logon/userlogon"); if(HTTPContext.Session["x-user-internal-flag"] == null) ctx.Result = new RedirectResult("logon/userlogon"); } public ActionResult Dashboard() { // nothing to see here return View(); }}\[/code\]I have cleaned up the code little bit to remove the debug.print statements.As per the logs I see that the \[code\]OnActionExecuting()\[/code\] method and \[code\]userInfo\[/code\] child action are invoked simultaneously. At one point \[code\]OnActionExecuting()\[/code\] gets nulls for session variables. In the log I can see that until the point \[code\]ChildAction\[/code\] is invoked, session variables hold their value within \[code\]OnActionExecuting()\[/code\]. Once the \[code\]childaction\[/code\] accesses them, they become null. When I comment the code that accesses session from child action, everything works fine. What am I doing wrong? Is there some precaution I have to take while accessing session variables? Is this due to my ignorance about how to access Session asynchronously?I also have following in my \[code\]web.config\[/code\]:\[code\]<modules runAllManagedModulesForAllRequests="true"/>\[/code\]
 
Back
Top