0

Asp.net MVC uses stateless protocol, if yes then how is the session gets sustained? to explain it in a better manner. I have two methods like this

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        HttpContext.Session["somename"] = "foo";
        return View();
    }
    public ActionResult SomeMoreIndex()
    {
        var name = (string)HttpContext.Session["somename"];
        return View("Index");
    }
}
  1. now, when i make a request home/index and check the sessionId using

HttpContext.Session.SessionID

i get this id "wfeprxbpbngr4jn24n1dttg1" but when i make a same request i get some other session id like this "oaxw3g4f5felo2nr0ly15dn4"

  1. as you can see i am storing some value in the session named "somename". now when i try to acess the session, i get the value which i have store in the method named Index even tough i get a different session Id for every request.

how is it possible?

Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
  • It's possible because that's how the developers of ASP.NET MVC wrote the code. – Robert Harvey Nov 18 '18 at 16:08
  • can u help me with some articles on how this is acheived? – Lijin Durairaj Nov 18 '18 at 16:10
  • 1
    ASP.NET MVC is not a stateless technology in and of itself, although it is often used in a stateless way. The HTTP Protocol which ASP.NET uses as its transport protocol is the ‘stateless part’ if you want to think of it like this. There is an explanation of this in [this answer](https://stackoverflow.com/a/23470127/465921). The reason for the session id change is explained in [this answer](https://stackoverflow.com/a/2874174/465921). – Thomas Nov 18 '18 at 17:15
  • Possible duplicate of [ASP.NET: Session.SessionID changes between requests](https://stackoverflow.com/questions/2874078/asp-net-session-sessionid-changes-between-requests) – Thomas Nov 18 '18 at 17:17
  • @Thomas, please read my question carefully, it is related to asp.net MVC and it is not about changing sessionID but about how does the session gets sustained in the MVC application if it is a stateless protocol – Lijin Durairaj Nov 19 '18 at 08:25
  • @LijinDurairaj I did read the question and still think the linked question is closely related to your issue. Sessions are maintained because MVC is not a 'stateless protocol'. Can you clarify the exact behavior you have: if you add the line `System.Diagnostics.Debug.WriteLine(HttpContext.Session.SessionID);` to the top of each of your actions, and then load the application and call the `Index` action followed immediately by the `SomeMoreIndex` action, is the output window in Visual Studio showing two different SessionID values? I cannot replicate this behavior from a vanilla/empty project. – Thomas Nov 19 '18 at 15:34
  • @Thomas, yes, i am getting two different sessionID – Lijin Durairaj Nov 21 '18 at 14:10

0 Answers0