State Management in ASP.NET and ASP.NET MVC

As we all know HTML is stateless language. It cannot persist state, so every web development framework provides facility to maintain state in one way or another way. There are different techniques available for state management in ASP.NET and ASP.NET MVC. Following is summary of all such techniques for maintaining state.

Client Based State Management Options

View State The ViewState property provides a dictionary object for retaining values between multiple requests for the same page.
Control State The ControlState property allows you to persist property information that is specific to a control and cannot be turned off like the ViewState property.
Hidden Fields A HiddenField control stores a single variable in its Value property and must be explicitly added to the page.
Cookies A cookie is a small amount of data that is stored either in a text file on the client file system or in-memory in the client browser session.
Query String A query string is information that is appended to the end of a page URL.
View Data(MVC) ViewData is a dictionary of objects accessible using strings as keys.
View Bag(MVC) ViewBag is a dynamic property. It does not require typecasting for complex objects.
Temp Data(MVC) TempData is a dictionary of string key and object value. TempData keep the information for the time of an HTTP Request. This mean only from one page to another.



Server Based State Management Options

Application State Application state is a global storage mechanism that is accessible from all pages in the Web application.
Session State Session state is similar to application state, except that it is scoped to the current browser session.
Profile Properties Profile Properties is similar to Session State. The profile-properties feature uses an ASP.NET profile, which is stored in a persistent format and associated with an individual user.
State Management in ASP.NET and ASP.NET MVC
0 votes, 0.00 avg. rating (0% score)

4 Comments

  1. Alex Synytsia

    Hi Jinal,

    Probably I am wrong, but I think, ViewData and ViewBag is not part of client side.
    It looks like they are client side, but they are actually properties of class System.Web.MVC.Controller (or ControllerBase). And since they are part of controller they are located on the server side, and cannot hold state since controllers are stateless (or in another word, you cannot link some particular controller to some particular page, and use it ViewBag or ViewData properties to store state of some particular page.
    Even more from my testing of MVC-4, we don’t know which controller instance will be used next time by the same page.
    By default controllers are created by System.Web.Mvc.DefaultControllerFactory, and I don’t know how many instances of the same controller class are created.
    I did simple experiment: created Web application with one page and one controller. In the controller I had property which is lazy initialized by Guid. I deployed application on the server and called it from different computers. Both pages on both PCs shows to me the same Guid. It means both pages from different PCs call the same controller on the server.
    In this case if I decide to use ViewBag or ViewData properties of that controller to store state (or to remember something about page), the controller will just remember information stored by last call.
    In order to use ViewBag or ViewData (or something else on controller side) as a state storage, we need to override class System.Web.Mvc.DefaultControllerFactory that it will produce some kind of statefull controllers which wil be created each instances for each page, and remember to which page they are linked (the some as we have in JAVA EE6 JSF – ManagedBeans)

    Thanks,
    Alex

    Reply

    • Jinal Desai

      Hi Alex,

      ViewData and ViewBag data is internally stored at client side browser cookies.

      Please find one interesting link I found in the codeproject here.

      Thanks.

      Reply

  2. Dilish

    Jinal,

    Just found your site when I was browsing through some MVC stuff.. You are doing a great job..!

    I agree with Alex. ViewData, ViewBag and TempData can be accessed from Server side only. The whole purpose of these options are just to pass values from controller to view/controller and that is just temporary, doesn’t requires persistence at all.

    If its getting stored in a cookie or accessible from client side, can’t we access it through javascript? This article shows that we cannot directly access it through javascript.

    http://stackoverflow.com/questions/10389649/possible-to-access-mvc-viewbag-object-from-javascript-file

    The article you pointed out in code project is talking about session in the last paragraph.
    Do you know any other article talking about storing these values in a cookie..?

    Thanks,

    Reply

    • Jinal Desai

      Hi Dilish,

      Your are right, all three ViewData, ViewBag and TempData are server side technology. But, here in this article I write from it’s storage perspective. All the three ViewData, ViewBag and TempData stored using short lived sessions which are stored using cookies.

      View State:
      By default it’s put into the rendered html as a hidden field. However you can override this default and put it in session by overriding the PageStatePersister property on the Page and make use of the SessionPageStatePersister class. You can also create your own custom storage if needed.
      http://forums.asp.net/t/1504590.aspx/1

      View Bag:
      It’s same as View Bag except it provides dynamic mechanism to store the values.

      Temp Date:
      By default, SessionStateTempDataProvider is used so by default TempData is stored into session. But you can implement your own TempData-provider.
      http://forums.asp.net/t/1473413.aspx

      Thanks

      Reply

Leave a Reply

*


*