New session created after RedirectToAction in MVC

Wojciech Rajchel 21 Reputation points

In an MVC sometimes the session is lost after RedirectToAction to an another controller. I have build in global exception logging in Global.asax in Application_Error but nothing is logged at the moment when session is lost.

The problem happens a few times a day. Can you point me how to proceed to capture the reason of this problem?

A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
846 questions
{count} votes

1 answer

Sort by: Most helpful
  1. QiYou-MSFT 2,761 Reputation points Microsoft Vendor

    Hi @Wojciech Rajchel

    First of all, the first point: since there is no cookie, then we write a cookie.

    HttpCookie aCookie = new HttpCookie("cookie1"); 
    aCookie.Value = "";
    aCookie.Expires = DateTime.Now.AddDays(1);

    Next, if you want all pages below your site to access this cookie. This is called the scope of the cookie, and you can set it like this:

    HttpCookie aCookie = new HttpCookie("cookie1"); 
    aCookie .Value = "written " + DateTime.Now.ToString(); 
    aCookie .Expires = DateTime.Now.AddDays(1); 
    aCookie .Path = "/";
    Response.Cookies.Add(aCookie );

    Best Regards

    Qi You

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.