Presentation is loading. Please wait.

Presentation is loading. Please wait.

Not like the State of Virginia. What is State in ASP.NET? Services (like web services) are Stateless. This means if you make a second request to a server,

Similar presentations


Presentation on theme: "Not like the State of Virginia. What is State in ASP.NET? Services (like web services) are Stateless. This means if you make a second request to a server,"— Presentation transcript:

1 Not like the State of Virginia

2 What is State in ASP.NET? Services (like web services) are Stateless. This means if you make a second request to a server, it will not remember what your last request was. ASP.NET adds the concept of State to web pages This allows you to have objects persist across page requests

3 Why Use State? Almost every page online uses state Its hard to find a meaningful static web page State allows pages to dynamically respond to different users and events Example Uses: Enforcing page authentication Remembering your shopping cart Personalized search results

4 State Objects Simply put: a collection of objects Collection Ex: Session[UserId] = 4; Objects: Session ViewState Application Cache Cookies

5 Session Lifetime: From the first request til timeout/cleared session It is possible for session to last longer than the server host if stored using SQL Session Storage Scope: Only to the owner of the session Persists across pages. Inaccessible from other sessions Usage: Accessing data shared across pages (ex. Login info)

6 ViewState Lifetime: The life of the page Rendered to the page Scope: Just that request. Does not persist across pages or across sessions Rebuilt for the following requests Usage: Control selection data (form data, checkbox selections, drop down list selections, etc.)

7 ViewState

8 Application Lifetime: The life of the application Persists until the service restarts Scope: The entire application All users in all sessions have access to this object Usage: Application wide settings Configuration settings

9 Cache Lifetime: Until the cached object expires Scope: Application wide All users in all sessions have access to this object Usage: Stored commonly accessed data

10 Cookies?

11 Cookies Lifetime: Lifetime of the cookie Be kind to your users. Dont forget to set an expiration date on your cookies Scope: The browser Can be accessed on any page in the cookies domain (determined by browser security settings) Usage: Quenching random hunger cravings

12 Dont do this:

13 Strongly Typed State Remember: a collection of objects Collection You can store any object in these collections What if you want to restrict the types objects stored in the State? Enforce type in a typeless environment Removes confusion about whats stored in the object

14 Strongly Typed State public int UserId { get { if (!(Session["UserId"] is int)) Session["UserId"] = -1; return (int)Session["UserId"]; } set { Session["UserId"] = value; }


Download ppt "Not like the State of Virginia. What is State in ASP.NET? Services (like web services) are Stateless. This means if you make a second request to a server,"

Similar presentations


Ads by Google