1 / 14

State Management

State Management. 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, it will not remember what your last request was. ASP.NET adds the concept of State to web pages

pier
Download Presentation

State Management

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. State Management 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 • It’s 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<string, object> • 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 <inputtype="hidden"name="__VIEWSTATE"id="__VIEWSTATE"value="/wEPDwUKMTc0NTQxNTAwMWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgMFKWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlcjEkQ2hlY2tCb3hMaXN0MSQwBSljdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJENoZWNrQm94TGlzdDEkMQUpY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRDaGVja0JveExpc3QxJDFN/3diiSeYfuwsveCFZkB8GZhziyh9fxFB4N3DEHI9Ow=="/>

  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. Don’t forget to set an expiration date on your cookies • Scope: • The browser • Can be accessed on any page in the cookie’s domain (determined by browser security settings) • Usage: • Quenching random hunger cravings

  12. Don’t do this:

  13. Strongly Typed State • Remember: a collection of objects • Collection<string, object> • 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 what’s stored in the object

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

More Related