Session State Module Example

This post will tell  you the example of Default, Disabled and Read-Only session module

Session State :  Default

Controller :

  [SessionState(System.Web.SessionState.SessionStateBehavior.Default)]
   public class TestController
   {
        public ActionResult TestSession()
        {
            HttpContext.Session["TestSession"] = "India";
            return View();
        }

   }

View :

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test Session</title>
</head>
<body>
         Session value "@Session["TestSession"].ToString()"
</body>
</html>

 

Session State :  Disable

Controller :

  [SessionState(System.Web.SessionState.SessionStateBehavior.Disable)]
   public class TestController
   {
        public ActionResult TestSessionAction()
        {
            HttpContext.Session["TestSession"] = "India";
            return View();
        }

   }

As in disable you cannot neither read nor write session. This code will throw the “Null Reference” exception.

Session State :  Read-Only

Suppose initial value HttpContext.Session[“TestSession”] is “India”.

Controller :

  [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
   public class TestController
   {
        public ActionResult TestUpdateSession()
        {
            HttpContext.Session["TestSession"] = "Pune";
            return View();
        }
        public ActionResult TestReadSession()
        {           
            return View();
        }
    }

 View :

TestUpdateSession

<html>
<head>   
    <title>Test Update Session</title>
</head>
<body>
         Session value "@Session["TestSession"].ToString()"
</body>
</html>

TestReadSession

<html>
<head>   
    <title>Test Read Session</title>
</head>
<body>
         Session value "@Session["TestSession"].ToString()"
</body>
</html>

Scenario 1:

1st request TestController/TestReadSession

o/p : Session value “India”

2nd request TestController/TestUpdateSession

o/p : Session value “Pune”

Scenario 2:

1st request TestController/TestUpdateSession

o/p : Session value “Pune”

2nd request TestController/TestReadSession

o/p : Session value “India”

Conclusion : As I said in my previous post that in read-only session is updated at only request level. See here, 1st request has update the session value but it has not affected the next request.

One can set the session state behavior on the controller i.e Session State Behavior can ONLY be set at Controller level NOT at Action level. There are times where controller is Read-Only and there is/are some action which wants to update the session therefore it want Session behavior to be set as Default. Because of one action we cannot change the behavior of other actions. So one way is to write one NEW controller with session state as Default and move that action in that controller. But this logic is not very much feasible.

So, How to solve this problem??  Always remember one think “Every problem has the solution :)”. Just you need to find out.

Hint: As my site slogan say’s “You Rock, when your basics Rocks”, solution is hidden in basic of C#. OVERIDDING.

Will tell  Solution you in my next post “Session State at Action level“.  🙂

Advertisements

2 thoughts on “Session State Module Example

  1. Pingback: Session State on Action | Technology At Your Finger Tips

  2. Pingback: Session State Module | Technology At Your Finger Tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s