Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Controlling Session Behavior in Asp.Net MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 528
    Comment on it

    Controlling Session Behavior in Asp.Net MVC


    Sessions in Asp.Net MVC are used across requests for storing data. Asp.Net MVC manages sessions for all controllers in the application irrespective of the fact data values are stored in the session or not therefore making it time consuming.The application performance is affected since session is stored on server side and server memory is consumed.

     


    Sessionless Controller

     

    There is a possibility that some of the controllers in the application does not make use of session state feature. In such scenario the controller session can be disabled. For disabling controller's session sessionState attribute is used for setting session state behavior to disabled as a result there is slight performance improvement in the application. System.Web.SessionState is required for controlling session state. 

     

    Example to demonstrate Sessionless Controller :-

     

    [SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]
    
    public class HomeController : Controller
    {
        // code       
    }	

     


    In above example by using sessionState attribute and marking it as disabled makes the controller sessionless.

     

    The specification of type of session support that is required for handling the request is termed as SessionStateBehavior. It can have different type of values :-

     

    • Default :  The session state behavior for the request can be determined by using the default ASP.NET logic. 
    • Disabled : The state of the session is disabled entirely.
    • ReadOnly : The behaviour of the sessionState becomes read only i.e data can be read from sessionState but data cannot be writtrn on the session.  
    • Required : The behaviour of the sessionState becomes full read-write. 

     

    An exception is thrown if the controller session is marked as disabled and session is accessed  within the controller or TempData make use of session state for  data values storage across requests.

     

    Example :-

     

    SessionState(System.Web.SessionState.SessionStateBehavior. Disabled)]
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Tesing";
            Session["test"] = "Testing of Session less controller";
            return View();
        }
    }

     

    In above example a NullReferenceException will be thrown where Session variable is used.

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: