14
State Management Not like the State of Virginia

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,

Embed Size (px)

Citation preview

Page 1: 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,

State ManagementNot like the State of Virginia

Page 2: 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,

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

Page 3: 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,

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

Page 4: 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,

State ObjectsSimply put: a collection of objects

Collection<string, object>Ex: Session[“UserId”] = 4;

Objects:SessionViewStateApplicationCacheCookies

Page 5: 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,

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)

Page 6: 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,

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.)

Page 7: 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,

ViewState<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTc0NTQxNTAwMWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgMFKWN0bDAwJENvbnRlbnRQbGFjZUhvbGRlcjEkQ2hlY2tCb3hMaXN0MSQwBSljdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJENoZWNrQm94TGlzdDEkMQUpY3RsMDAkQ29udGVudFBsYWNlSG9sZGVyMSRDaGVja0JveExpc3QxJDFN/3diiSeYfuwsveCFZkB8GZhziyh9fxFB4N3DEHI9Ow==" />

Page 8: 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,

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

Page 9: 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,

CacheLifetime:

Until the cached object expiresScope:

Application wideAll users in all sessions have access to this

objectUsage:

Stored commonly accessed data

Page 10: 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,

Cookies?

Page 11: 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,

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

Page 12: 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,

Don’t do this:

Page 13: 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,

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

Page 14: 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,

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