ASP.NET state management

4/4/2014·7 min read

There're variety of ways tp store variables on ASP.NET, such as files, and database, server memory, hidden, session, cache and more. Also, how to store variables could be be classified is divided into client side and server side.

As how to save the value of the client, view state, HiddenField, control state, cookies, Query String, and files, javascript.

  • view state:similar HiddenField, but Microsoft mapped Input Hidden HTML into ASP.NET. The difference is that they can be used as delivery of information security and encryption. The disadvantage is that the amount of information sent to the server with encryption and decryption time that the amount of information, to send to the server by encrypting increases will increase. Usually, using for postback to server side. Therefore, when you pass a value to another page will not be available for ViewState. ※ However, because you can post back the data to another page in ASP.NET, in that case will be able to use view state is internally.
  • HiddenField: Html input element, type is hidden. HiddenField of HTML can not be encrypted, it would have been better to pass information to the server involved in security is not used. HiddenField could use as a postback in its page and send a value to another page as same way as view state. ※ How do you post back to other page? you could change your Button's PostBackUrl's to other page.)
  • Control state: Use Control State, when ViewState Setting is invalid, which you could set ViewState on page or site level. It's usually used on create custom control primarily
  • cookies: You can pass values ​​between pages in the application. Be able to track the value of the logged in user to use along with the session. One example to use is, After logging in the site such as Facebook and Google, have issued the cookie from the server to the client, the client's session subsequent to acknowledge that the use of which session to determine the cookies received from the client. The disadvantage, because users can disable cookies in client's browsing, it is necessary to consider how to respond at that time. For example, to use the Query String.
  • QueryString: You can pass a value to a page within a page or another. How to set the value to set the keys and values ​​behind the URL. This method is server-side check of values ​​is necessary. On the plus side it can be stored in the Favorites set to URL search information, you could also put to your e-mail, in case if you want to show your user the querystring.
  • Javascript Cache You could also save your value at javascript, and use at your javascript logic.
  • ※ The above method is a technique ASP.NET.
  • Session state: to be used when you want to save the information between different users in the Lord. By utilizing the above in conjunction with cookies, cookies to the client (eg, 0001) by passing the (0001 "Warriors" in the user session information is Tanaka) to use the session that it was on the server side to determine the . ASP.NET can also disable the session. If there is a way to enable the use of many kinds. 1. InProc: how to save a session in the InProcess. The number of users of the application without a lot, if you can accommodate in a single application server is available on how to set this up. In this case, the session can be stored in the memory of that server. This setting is the default setting, seems to get the most performance. 
    2. SQLServer: SQLServer to store the session. If you have a large number of users will be able to use this method. SQLServer is also possible to save multiple. Therefore, this method will be to save the session to multiple servers such as web farm. If you want to save the value, even when the application is restarted is also available. Coordination between servers is possible if you use a load balancer tool. 
    3. State Servers: state can be stored on multiple servers to the same session as SQLServer, when the user mass. If you want to save the value, even when the application is restarted is also available. There is also need to use load Lancer van with it. 
    4. Custom: This setting is available if you save the session in a different way. For example, if you want to save the value to something custom, or if you want to save the Oracle. 
  • The application states: If you have similar ー cache and save them to memory the value of the common application server, multiple users can use in common. When you save the file and get the database value is better to use a cache ー there is a variety of settings that will be good. In addition, the location where you want to use is a good start in the global event applications of ASP.NET, the end.
  • Cache: The cache ー There are two kinds. There is a page output cache ー ー and application cache. The application cache is ー it can be utilized in applications to all users get the database value, that can be used in common to cache the XML file, for example ー such as a file.ー can cache various settings. Can, for example, the disabled or the time or to disable the cache ー A change in the value of the file server or a cache ー fixed. Page output cache can improve performance ー by the use of a cache of the server when ー the next time the user accesses to cache web pages ー on the application server. Page output cache is ー ー It also can be cached page is dynamic change, you can set the cache time ー ー same as the application cache. Because there is a cache item ー If you search on Google, the page display by pressing the cache is said to be ー open a page that is cached in the application server ー Google.
  • Profile: It is to be used in conjunction with SQLServer is common. For example, I think that the information that you want to save screen name, such as color, such as per-user information, IP address of your choice. To be displayed it in the code for the ASP.NET Web.Config to define these items in the item. For example, Profile.name, such as profile.ipaddress, if you set the value after these from the screen, the value is automatically stored in SqlServer. This method is also fixed column tables of SQLServer.
  • Files: there is a way to encrypt or stored in a database to store a value in the XML file of the application WebConfig and, also, such as text or Excel file otherwise. 
    How do I save the client's Excel, text files, binary files as some would. If you want to save information to the client continuously in something would be available. For example, when the user's PC to save the values ​​that you register, to register from the next time because there is continued potential for many users quit in the middle there is a registration application shall be read, followed by information to be registered across multiple screens, the screen would be able to save files of something, if the binary file was used to. And how to save the value of the server, the session state, application state, cache ー, and profiles and then file. 

Inclusion, information security is to save into the database or server will best. However, it is heavy load on the server and stored on the server to all, to handle many users is a time consuming, but effective use of distributed to the client is the best.