Saturday, February 5, 2011

ASP.NET state management

Que: What is a PostBack?
Ans: Send client side information/Data to server.

Que: HTTP protocol is stateless,so how do we maintain state between client/server on internet?
Ans: There are different ways to maintain states across Internet
Client: ViewState,Cookie,QueryString,Hidden fields,Control State
Server: Application, Session, Profile, DataBase

Que: What is ViewState?
Ans: ViewState the the way provided by ASP.NET to remember the state in postback.Because HTTP is stateless protocol, and every request to the server is a fresh request. So in order to maintain any data on the page(on client side), we have feature provided by ASP.NET,that is called as view state. It is stored in encrypted format. It is used by different ASP controls to store the state of the control and data to be preserved after postback.

Que: What are advantages and disadvantages of viewstate?
Ans: Advantages:
a) The view state is contained in a structure within the page code,so no server resources are required:
b) Enhanced security features: The values in view state are hashed, compressed, and encoded for Unicode implementations.
Disadvantages:
a) Performance considerations: Because the view state is stored in the page itself, storing large values can cause the page to slow down when users display it and when they post it.
b) Security risks: The view state is stored in one or more hidden fields on the page. Although view state stores data in a hashed format, it can still be tampered with.

Que: How Cookie works?
Ans: Cookie is small amount of data maintain by client browser in text file(or client file system/in-memory). When the user visits any site first time,the server sends cookie along with the web page.This cookie get saved on client side.Next time any request goes to the same website(need not to be the same page) the cookie goes along with the request and server could authenticate the user based upon cookie.

Que: What are the type of Cookies?
Ans: There are 2 type of cookies:
1) Persistent: These cookies have expiration time set, and saved on hard disk. These will be available across browser instance also but only upto expiration time. The expired cookie gets deleted when user visit to the same site again who created this cookie.
2) Non-Persistent: Cookie that is not stored on user's hard disk, because they don't have any expiration time.These are part of browsers only.As soon as browser is closed, the cookie will be gone. It can not be shared across different browser instances.

Que: Where are cookies stored in your computer?
Ans: C:\Documents and Settings\[ your account name ]\Cookies

Que: What are advantage and disadvantage of cookie?
Ans: Advantages:
a) Configurable expiration rules
b) No server resources are required
c) Simplicity:Cookies are simple to write and handle.
Disadvantages:
a) Size limitations
b) User can disable their cookies by browser settings
c) Security risk: Cookies can easily be tampered.

Que: How to use Query Strings for state management?
Ans: We append information at the end of URL. Ex:
http://www.pankajsoni.com/abc.aspx?myKey1=myValue1&myKey2=myValue2

Que: Explain server side state management.
Ans:
Application state: Application state is stored in Application property of HttpContext. It is stored in an instance of HttpApplicationState class(a key-value dictionary). There are 2 ways to use application states.
1) Put the application state data in Application_start in Global.asax.
2) Add data in StaticObject collection by in Global.asax file. Advantages: 1) This is stored in server memory, so much faster than saving data to disk or database. 2) The data can be accessed by multiple threads at the same time. Disadvantages: 1) On application start up the data will be lost. 2) If the application is using multiple server for load balancing then this data won't be available to other servers.
Session State: Session variables are stored in SessionStateItemCollection object exposed through HttpContext.Session property.
Ex: Session["MyKey"] = "MyValue";
The sessions are available for limited time window.
Advantages:
1) Data preserved even if we restart iis or worker process. Because data stored in another process space.
2) It can be used for those browsers also which doesn't support HTTP cookies.
3) It can be accessed from multiple servers.
Disadvantages:
1) Session state remains in memory until removed/replaced so degrade the server performance.
Profile: Profile properties are used to store user specific data. It is similar to session state unlike session, the profile is not lost when user's session expires. To use this property we need to enable it through web.config file.
DataBase: Server save all the information in Database.
Advantages:
1) More secure, More storage, Robustness
2) Accessible for multiple resources.
Disadvantages:
1) Complex structure
2) Performance degradation