150 likes | 260 Views
This article explores state management in ASP.NET, contrasting it with traditional web services. It highlights the importance of state in creating dynamic, responsive web pages by allowing objects to persist across requests. Key concepts include session management, ViewState, application scope, and caching. Real-world applications, such as user authentication and shopping cart functionality, illustrate how state enhances user experiences. Emphasizing the relevance of strong typing in state objects, the piece offers insights into effectively managing state in web applications.
E N D
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 • This allows you to have objects persist across page requests
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
State Objects • Simply put: a collection of objects • Collection<string, object> • Ex: Session[“UserId”] = 4; • Objects: • Session • ViewState • Application • Cache • Cookies
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)
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.)
ViewState <inputtype="hidden"name="__VIEWSTATE"id="__VIEWSTATE"value="/wEPDwUKMTc0NTQxNTAwMWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgMFKWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlcjEkQ2hlY2tCb3hMaXN0MSQwBSljdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJENoZWNrQm94TGlzdDEkMQUpY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRDaGVja0JveExpc3QxJDFN/3diiSeYfuwsveCFZkB8GZhziyh9fxFB4N3DEHI9Ow=="/>
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
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
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
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
Strongly Typed State publicintUserId { get { if (!(Session["UserId"] isint)) Session["UserId"] = -1; return (int)Session["UserId"]; } set { Session["UserId"] = value; } }