Date post: | 29-Mar-2015 |
Category: |
Documents |
Upload: | simone-hicken |
View: | 212 times |
Download: | 0 times |
State ManagementNot 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 pagesThis 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 pageState allows pages to dynamically respond to
different users and eventsExample Uses:
Enforcing page authenticationRemembering your shopping cartPersonalized search results
State ObjectsSimply put: a collection of objects
Collection<string, object>Ex: Session[“UserId”] = 4;
Objects:SessionViewStateApplicationCacheCookies
SessionLifetime:
From the first request til timeout/cleared sessionIt is possible for session to last longer than the
server host if stored using SQL Session StorageScope:
Only to the owner of the sessionPersists across pages. Inaccessible from other
sessionsUsage:
Accessing data shared across pages (ex. Login info)
ViewStateLifetime:
The life of the pageRendered to the page
Scope: Just that request. Does not persist across pages or
across sessionsRebuilt for the following requests
Usage:Control selection data (form data, checkbox
selections, drop down list selections, etc.)
ViewState<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTc0NTQxNTAwMWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgMFKWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlcjEkQ2hlY2tCb3hMaXN0MSQwBSljdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJENoZWNrQm94TGlzdDEkMQUpY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRDaGVja0JveExpc3QxJDFN/3diiSeYfuwsveCFZkB8GZhziyh9fxFB4N3DEHI9Ow==" />
ApplicationLifetime:
The life of the application Persists until the service restarts
Scope:The entire applicationAll users in all sessions have access to this
objectUsage:
Application wide settingsConfiguration settings
CacheLifetime:
Until the cached object expiresScope:
Application wideAll users in all sessions have access to this
objectUsage:
Stored commonly accessed data
Cookies?
CookiesLifetime:
Lifetime of the cookie Be kind to your users. Don’t forget to set an
expiration date on your cookiesScope:
The browserCan be accessed on any page in the cookie’s
domain (determined by browser security settings)Usage:
Quenching random hunger cravings
Don’t do this:
Strongly Typed StateRemember: a collection of objects
Collection<string, object>You can store any object in these collectionsWhat 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 Statepublic int UserId{ get { if (!(Session["UserId"] is int)) Session["UserId"] = -1; return (int)Session["UserId"]; } set { Session["UserId"] = value; }}