SlideShare a Scribd company logo
ASP.NET
State Management
Presented BY
A.M Rajole
Lecture Overview
 Client state management options
 Cookies
 Server state management options
 Application state
 Session state
 View state
Introduction to State
Management
 Remember that ASP.NET (and the Web) is
stateless
 The Web server does not keep track of past
client requests
 Different technologies handle the issue of
statement management differently
 ASP.NET is somewhat unique in this regard
 But PHP works similarly
Types of State Management
 ASP.NET offers two categories of state
management
 Pure client-side statement management
 Server-side state management
 We can use both concurrently
State Management (Issues)
 Client state management consumes
bandwidth and introduces security risks
because sensitive data is passed back and
forth with each page postback
 Preserving state on a server can overburden
servers
 We also must consider Web farms and Web
gardens
Web Farm
 A single ASP.NET application is hosted on
multiple web servers
Web Garden
 A single application pool contains multiple
worker processes run across multiple CPUs
Client State Management
(Introduction)
 View state
 Control state
 Hidden fields
 Cookies
 Query strings
State Management
(ViewState)
 ViewState works in a couple of different
ways
 It’s managed for you automatically via the
ASP.NET infrastructure
 Or you can take control yourself via the
ViewState object
 ViewState only provides state for a single
page
 It won’t work with cross page postbacks or
other page transfers
State Management
(ViewState) (auto)
 Just enable ViewState and the corresponding
control retains is value from one postback to the next
 This happens by default
 You can disable ViewState at the page level
 <%@ Page EnableViewState=”false” %>
 While simple, it does add overhead in terms of page
size
 MaxPageStateFieldLength controls the
maximum page size
State Management
(ViewState) (manual)
 ViewState can be set on the server up to
the Page_PreRenderComplete event
 You can save any serializable object to
ViewState
 The information is really just saved in hidden
fields
 See example ViewState.aspx
State Management
(ControlState)
 The ControlState property allows you to
persist information as serialized data
 It’s used with custom controls (UserControl)
 The wiring is not automatic
 You must program the persisted data each
round trip
 The ControlState data is stored in hidden
fields
 More later when (if) we create a user control
State Management
(Hidden Fields)
 Use the HiddenField control to store
persisted data
 The data is stored in the Value property
 It’s simple and requires almost no server
resources
 It’s available from the ToolBox and works
much like a text box control
State Management (Cookies)
 Use to store small amounts of frequently
changed data
 Data is stored on the client’s hard disk
 A cookie is associated with a particular URL
 All data is maintained as client cookies
 Most frequently, cookies store personalization
information
Cookie Limitations
 While simple, cookies have disadvantages
 A cookie can only be 4096 bytes in size
 Most browsers restrict the total number of
cookies per site
 Users can refuse to accept cookies so don’t
try to use them to store critical information
Cookies Members
 Use the Response.Cookies collection to
reference a cookie
 Value contains the cookie’s value
 Expires contains the cookie’s expiration date
 Cookies can also store multiple values using
subkeys
 Similar to a QueryString
 It’s possible to restrict cookie scope by setting
the Path property to a folder
 See example cookies.aspx
State Management
(Query Strings)
 As you know, query strings are just strings
appended to a URL
 All browsers support them and no server
resources are required
State Management
(Query Strings)
 HttpContext.Current.Request.RawURL
gets a URL posted to the server
 Or just use Request.QueryString
 HttpUtility.ParseQueryString breaks
a query string into key / value pairs
 It returns a NameValueCollection
 We will see how to do this later when we
work more with query strings an URLs
Server State Management
Options
 Application state
 Session state
 Profile properties
 Database support
Server State Management
 HttpApplicationState applies to your
entire application
 Application object
 HttpSessionState applies to the
interaction between a user’s browser session
and your application
 Session object
 Page caching also relates to state
management
The Application’s State
(Introduction)
 An instance of the HttpApplicationState
object is created the first time a client
requests a page from a virtual directory
 Application state does not work in a Web farm
or Web garden scenario
 Unless we push data to a database
 It’s volatile – If the server crashes, the data is
gone
 Unless we persist out of process
 It’s really just a collection of key/value pairs
The HttpApplicationState
Class (Members 1)
 The AllKeys property returns an array of
strings containing all of the keys
 Count gets the number of objects in the
collection
 Item provides read/write access to the
collection
The HttpApplicationState
Class (Members 2)
 StaticObjects returns a reference to
objects declared in the global.asax file
 <object> tags with the scope set to
Application
 The Add method adds a new value to the
collection
 The Remove method removes the value
associated with a key
The HttpApplicationState
Class (Members 3)
 Lock and Unlock lock the collection,
respectively
 Not an ASP topic but a thread synchronization
topic
 Get returns the value of a collection item
 The item can be referenced by string key or
ordinal index
 Set store a value corresponding to the
specified key
 The method is thread safe
Application State
(Best Practices)
 Memory to store application state information
is permanently allocated
 You must write explicit code to release that
memory
 So don’t try to persist too much application state
information
 The Cache object, discussed later, provides
alternatives to storing application state
information
Profile Properties
 It’s a way to permanently store user-specific
data
 Data is saved to a programmer-defined data
store
 It takes quite a bit of programming
 The whole thing is unique to windows
 We will not use profiles in this course
Profiles (Enabling)
 Web.config must be modified to enable
profiles
 You can then reference with code as follows
Session State (Introduction)
 It is used to preserve state for a user’s
‘session’
 Session state works in Web farm or Web
garden scenarios
 Since the data is persisted in the page
 Session data can be stored in databases such
as SQL Server or Oracle making it persistent
 It’s very powerful and the features have been
significantly enhanced in ASP 2.0
Session State (Configuration)
 The <web.config> file contains a
<sessionState> section
 The entries in this section configure the state
client manager
Web.Config
<sessionState>
<sessionState mode="[Off|InProc|StateServer|SQLServer|Custom]"
timeout="number of minutes" cookieName="session identifier
cookie name" cookieless=
"[true|false|AutoDetect|UseCookies|UseUri|UseDeviceProfile]
" regenerateExpiredSessionId="[True|False]"
sqlConnectionString="sql connection string"
sqlCommandTimeout="number of seconds"
allowCustomSqlDatabase="[True|False]"
useHostingIdentity="[True|False]"
stateConnectionString="tcpip=server:port"
stateNetworkTimeout="number of seconds"
customProvider="custom provider name">
<providers>...</providers> </sessionState>
Session State Providers (1)
 Custom – State information is saved in a
custom data store
 InProc – State information is preserved in
the ASP.NET worker process named
(aspnet_wp.exe or w3wp.exe)
 This is the default option
 Off – Session state is disabled
Session State Providers (2)
 SQLServer – State data is serialized and
stored in an SQL server instance
 This might be a local or remote SQL Server
instance
 StateServer – State information is stored
in a separate state server process
(aspnet_state.exe)
 This process can be run locally or on another
machine
Session State Providers
(Best Practices)
 Use an SQL server provider to ensure that
session state is preserved
 Note that there is a performance hit here
because the session information is not stored in
the page
 The InProc provider is not perfect
 The ASP worker process might restart thereby
affecting session state
Session State Identifiers
 Browser sessions are identified with a unique
identifier stored in the SessionID property
 The SessionID is transmitted between the
browser and server via
 A cookie if cookies are enabled
 The URL if cookies are disabled
 Set the cookieless attribute to true in the
sessionState section of the Web.config file
HttpSessionState
(Members 1)
 CookieMode describes the application’s
configuration for cookieless sessions
 IsCookieless is used to depict whether
cookies are used to persist state
 IsNewSession denotes whether the session
was created with this request
 SessionID gets the unique ID associated
with this session
 Mode denotes the state client manager being
used
HttpSessionState
(Members 2)
 Timeout contains the number of minutes to
preserve state between requests
 Abandon sets an internal flag to cancel the
current session
 Add and Remove add or remove an item to
the session state
 Item reads/writes a session state value
 You can use a string key or ordinal index value
Thank You

More Related Content

Similar to StateManagement in ASP.Net.ppt

State management
State managementState management
State management
Lalit Kale
 
IEEE KUET SPAC presentation
IEEE KUET SPAC  presentationIEEE KUET SPAC  presentation
IEEE KUET SPAC presentation
ahsanmm
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
Julie Iskander
 
Session viii(state mngtclient)
Session viii(state mngtclient)Session viii(state mngtclient)
Session viii(state mngtclient)
Shrijan Tiwari
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questions
Akhil Mittal
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3Neeraj Mathur
 
ASP.NET 12 - State Management
ASP.NET 12 - State ManagementASP.NET 12 - State Management
ASP.NET 12 - State Management
Randy Connolly
 
State Management In ASP.NET And ASP.NET MVC
State Management In ASP.NET And ASP.NET MVCState Management In ASP.NET And ASP.NET MVC
State Management In ASP.NET And ASP.NET MVC
jinaldesailive
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Mani Chaubey
 
State management
State managementState management
State managementIblesoft
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07Niit Care
 
State management in asp.net
State management in asp.netState management in asp.net
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
Peter Gfader
 
Managing states
Managing statesManaging states
Managing states
Paneliya Prince
 

Similar to StateManagement in ASP.Net.ppt (20)

State management
State managementState management
State management
 
IEEE KUET SPAC presentation
IEEE KUET SPAC  presentationIEEE KUET SPAC  presentation
IEEE KUET SPAC presentation
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
Session viii(state mngtclient)
Session viii(state mngtclient)Session viii(state mngtclient)
Session viii(state mngtclient)
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questions
 
ASP.Net Presentation Part3
ASP.Net Presentation Part3ASP.Net Presentation Part3
ASP.Net Presentation Part3
 
ASP.NET 12 - State Management
ASP.NET 12 - State ManagementASP.NET 12 - State Management
ASP.NET 12 - State Management
 
State Management In ASP.NET And ASP.NET MVC
State Management In ASP.NET And ASP.NET MVCState Management In ASP.NET And ASP.NET MVC
State Management In ASP.NET And ASP.NET MVC
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 
State management
State managementState management
State management
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07
 
Chapter 8 part2
Chapter 8   part2Chapter 8   part2
Chapter 8 part2
 
MVC
MVCMVC
MVC
 
State management in asp.net
State management in asp.netState management in asp.net
State management in asp.net
 
Ch05 state management
Ch05 state managementCh05 state management
Ch05 state management
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
Managing states
Managing statesManaging states
Managing states
 
Chapter 8 part1
Chapter 8   part1Chapter 8   part1
Chapter 8 part1
 

More from charusharma165

NETOverview1ppt.pptx
NETOverview1ppt.pptxNETOverview1ppt.pptx
NETOverview1ppt.pptx
charusharma165
 
Cartoon Training Courseware for Children.pptx
Cartoon Training Courseware for Children.pptxCartoon Training Courseware for Children.pptx
Cartoon Training Courseware for Children.pptx
charusharma165
 
reiniforcement learning.ppt
reiniforcement learning.pptreiniforcement learning.ppt
reiniforcement learning.ppt
charusharma165
 
ppt2.pdf
ppt2.pdfppt2.pdf
ppt2.pdf
charusharma165
 
JavaVariablesTypes.pptx
JavaVariablesTypes.pptxJavaVariablesTypes.pptx
JavaVariablesTypes.pptx
charusharma165
 
Artificial_intelligence.pptx
Artificial_intelligence.pptxArtificial_intelligence.pptx
Artificial_intelligence.pptx
charusharma165
 

More from charusharma165 (6)

NETOverview1ppt.pptx
NETOverview1ppt.pptxNETOverview1ppt.pptx
NETOverview1ppt.pptx
 
Cartoon Training Courseware for Children.pptx
Cartoon Training Courseware for Children.pptxCartoon Training Courseware for Children.pptx
Cartoon Training Courseware for Children.pptx
 
reiniforcement learning.ppt
reiniforcement learning.pptreiniforcement learning.ppt
reiniforcement learning.ppt
 
ppt2.pdf
ppt2.pdfppt2.pdf
ppt2.pdf
 
JavaVariablesTypes.pptx
JavaVariablesTypes.pptxJavaVariablesTypes.pptx
JavaVariablesTypes.pptx
 
Artificial_intelligence.pptx
Artificial_intelligence.pptxArtificial_intelligence.pptx
Artificial_intelligence.pptx
 

Recently uploaded

How to create an effective K-POC tutorial
How to create an effective K-POC tutorialHow to create an effective K-POC tutorial
How to create an effective K-POC tutorial
vencislavkaaa
 
Personal Brand Exploration Comedy Jxnelle.
Personal Brand Exploration Comedy Jxnelle.Personal Brand Exploration Comedy Jxnelle.
Personal Brand Exploration Comedy Jxnelle.
alexthomas971
 
Midterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptxMidterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptx
Sheldon Byron
 
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid feverWidal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
taexnic
 
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
foismail170
 
DOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdf
DOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdfDOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdf
DOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdf
Pushpendra Kumar
 
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
foismail170
 
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
Chapters 3  Contracts.pptx Chapters 3  Contracts.pptxChapters 3  Contracts.pptx Chapters 3  Contracts.pptx
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
Sheldon Byron
 
Heidi Livengood Resume Senior Technical Recruiter / HR Generalist
Heidi Livengood Resume Senior Technical Recruiter / HR GeneralistHeidi Livengood Resume Senior Technical Recruiter / HR Generalist
Heidi Livengood Resume Senior Technical Recruiter / HR Generalist
HeidiLivengood
 
131. Reviewer Certificate in BP International
131. Reviewer Certificate in BP International131. Reviewer Certificate in BP International
131. Reviewer Certificate in BP International
Manu Mitra
 
134. Reviewer Certificate in Computer Science
134. Reviewer Certificate in Computer Science134. Reviewer Certificate in Computer Science
134. Reviewer Certificate in Computer Science
Manu Mitra
 
My Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya WestrinMy Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya Westrin
AlinaseFaith
 
Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!
LukeRoyak
 
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdfDr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam
 
How Mentoring Elevates Your PM Career | PMI Silver Spring Chapter
How Mentoring Elevates Your PM Career | PMI Silver Spring ChapterHow Mentoring Elevates Your PM Career | PMI Silver Spring Chapter
How Mentoring Elevates Your PM Career | PMI Silver Spring Chapter
Hector Del Castillo, CPM, CPMM
 
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
foismail170
 
Operating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdfOperating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdf
harikrishnahari6276
 
salivary gland disorders.pdf nothing more
salivary gland disorders.pdf nothing moresalivary gland disorders.pdf nothing more
salivary gland disorders.pdf nothing more
GokulnathMbbs
 
Personal Brand exploration KE.pdf for assignment
Personal Brand exploration KE.pdf for assignmentPersonal Brand exploration KE.pdf for assignment
Personal Brand exploration KE.pdf for assignment
ragingokie
 
Interactive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaa
Interactive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaaInteractive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaa
Interactive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaa
23211a7274
 

Recently uploaded (20)

How to create an effective K-POC tutorial
How to create an effective K-POC tutorialHow to create an effective K-POC tutorial
How to create an effective K-POC tutorial
 
Personal Brand Exploration Comedy Jxnelle.
Personal Brand Exploration Comedy Jxnelle.Personal Brand Exploration Comedy Jxnelle.
Personal Brand Exploration Comedy Jxnelle.
 
Midterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptxMidterm Contract Law and Adminstration.pptx
Midterm Contract Law and Adminstration.pptx
 
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid feverWidal Agglutination Test: A rapid serological diagnosis of typhoid fever
Widal Agglutination Test: A rapid serological diagnosis of typhoid fever
 
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
欧洲杯投注app-欧洲杯投注app推荐-欧洲杯投注app| 立即访问【ac123.net】
 
DOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdf
DOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdfDOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdf
DOC-20240602-WA0001..pdf DOC-20240602-WA0001..pdf
 
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
太阳城娱乐-太阳城娱乐推荐-太阳城娱乐官方网站| 立即访问【ac123.net】
 
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
Chapters 3  Contracts.pptx Chapters 3  Contracts.pptxChapters 3  Contracts.pptx Chapters 3  Contracts.pptx
Chapters 3 Contracts.pptx Chapters 3 Contracts.pptx
 
Heidi Livengood Resume Senior Technical Recruiter / HR Generalist
Heidi Livengood Resume Senior Technical Recruiter / HR GeneralistHeidi Livengood Resume Senior Technical Recruiter / HR Generalist
Heidi Livengood Resume Senior Technical Recruiter / HR Generalist
 
131. Reviewer Certificate in BP International
131. Reviewer Certificate in BP International131. Reviewer Certificate in BP International
131. Reviewer Certificate in BP International
 
134. Reviewer Certificate in Computer Science
134. Reviewer Certificate in Computer Science134. Reviewer Certificate in Computer Science
134. Reviewer Certificate in Computer Science
 
My Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya WestrinMy Story of Getting into Tech By Gertrude Chilufya Westrin
My Story of Getting into Tech By Gertrude Chilufya Westrin
 
Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!Luke Royak's Personal Brand Exploration!
Luke Royak's Personal Brand Exploration!
 
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdfDr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
Dr. Nazrul Islam, Northern University Bangladesh - CV (29.5.2024).pdf
 
How Mentoring Elevates Your PM Career | PMI Silver Spring Chapter
How Mentoring Elevates Your PM Career | PMI Silver Spring ChapterHow Mentoring Elevates Your PM Career | PMI Silver Spring Chapter
How Mentoring Elevates Your PM Career | PMI Silver Spring Chapter
 
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
欧洲杯投注网站-欧洲杯投注网站推荐-欧洲杯投注网站| 立即访问【ac123.net】
 
Operating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdfOperating system. short answes and Interview questions .pdf
Operating system. short answes and Interview questions .pdf
 
salivary gland disorders.pdf nothing more
salivary gland disorders.pdf nothing moresalivary gland disorders.pdf nothing more
salivary gland disorders.pdf nothing more
 
Personal Brand exploration KE.pdf for assignment
Personal Brand exploration KE.pdf for assignmentPersonal Brand exploration KE.pdf for assignment
Personal Brand exploration KE.pdf for assignment
 
Interactive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaa
Interactive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaaInteractive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaa
Interactive Dictionary AIDS-B.pptx aaaaaaaaaaaaaaaaaaaaaaaaaa
 

StateManagement in ASP.Net.ppt

  • 2. Lecture Overview  Client state management options  Cookies  Server state management options  Application state  Session state  View state
  • 3. Introduction to State Management  Remember that ASP.NET (and the Web) is stateless  The Web server does not keep track of past client requests  Different technologies handle the issue of statement management differently  ASP.NET is somewhat unique in this regard  But PHP works similarly
  • 4. Types of State Management  ASP.NET offers two categories of state management  Pure client-side statement management  Server-side state management  We can use both concurrently
  • 5. State Management (Issues)  Client state management consumes bandwidth and introduces security risks because sensitive data is passed back and forth with each page postback  Preserving state on a server can overburden servers  We also must consider Web farms and Web gardens
  • 6. Web Farm  A single ASP.NET application is hosted on multiple web servers
  • 7. Web Garden  A single application pool contains multiple worker processes run across multiple CPUs
  • 8. Client State Management (Introduction)  View state  Control state  Hidden fields  Cookies  Query strings
  • 9. State Management (ViewState)  ViewState works in a couple of different ways  It’s managed for you automatically via the ASP.NET infrastructure  Or you can take control yourself via the ViewState object  ViewState only provides state for a single page  It won’t work with cross page postbacks or other page transfers
  • 10. State Management (ViewState) (auto)  Just enable ViewState and the corresponding control retains is value from one postback to the next  This happens by default  You can disable ViewState at the page level  <%@ Page EnableViewState=”false” %>  While simple, it does add overhead in terms of page size  MaxPageStateFieldLength controls the maximum page size
  • 11. State Management (ViewState) (manual)  ViewState can be set on the server up to the Page_PreRenderComplete event  You can save any serializable object to ViewState  The information is really just saved in hidden fields  See example ViewState.aspx
  • 12. State Management (ControlState)  The ControlState property allows you to persist information as serialized data  It’s used with custom controls (UserControl)  The wiring is not automatic  You must program the persisted data each round trip  The ControlState data is stored in hidden fields  More later when (if) we create a user control
  • 13. State Management (Hidden Fields)  Use the HiddenField control to store persisted data  The data is stored in the Value property  It’s simple and requires almost no server resources  It’s available from the ToolBox and works much like a text box control
  • 14. State Management (Cookies)  Use to store small amounts of frequently changed data  Data is stored on the client’s hard disk  A cookie is associated with a particular URL  All data is maintained as client cookies  Most frequently, cookies store personalization information
  • 15. Cookie Limitations  While simple, cookies have disadvantages  A cookie can only be 4096 bytes in size  Most browsers restrict the total number of cookies per site  Users can refuse to accept cookies so don’t try to use them to store critical information
  • 16. Cookies Members  Use the Response.Cookies collection to reference a cookie  Value contains the cookie’s value  Expires contains the cookie’s expiration date  Cookies can also store multiple values using subkeys  Similar to a QueryString  It’s possible to restrict cookie scope by setting the Path property to a folder  See example cookies.aspx
  • 17. State Management (Query Strings)  As you know, query strings are just strings appended to a URL  All browsers support them and no server resources are required
  • 18. State Management (Query Strings)  HttpContext.Current.Request.RawURL gets a URL posted to the server  Or just use Request.QueryString  HttpUtility.ParseQueryString breaks a query string into key / value pairs  It returns a NameValueCollection  We will see how to do this later when we work more with query strings an URLs
  • 19. Server State Management Options  Application state  Session state  Profile properties  Database support
  • 20. Server State Management  HttpApplicationState applies to your entire application  Application object  HttpSessionState applies to the interaction between a user’s browser session and your application  Session object  Page caching also relates to state management
  • 21. The Application’s State (Introduction)  An instance of the HttpApplicationState object is created the first time a client requests a page from a virtual directory  Application state does not work in a Web farm or Web garden scenario  Unless we push data to a database  It’s volatile – If the server crashes, the data is gone  Unless we persist out of process  It’s really just a collection of key/value pairs
  • 22. The HttpApplicationState Class (Members 1)  The AllKeys property returns an array of strings containing all of the keys  Count gets the number of objects in the collection  Item provides read/write access to the collection
  • 23. The HttpApplicationState Class (Members 2)  StaticObjects returns a reference to objects declared in the global.asax file  <object> tags with the scope set to Application  The Add method adds a new value to the collection  The Remove method removes the value associated with a key
  • 24. The HttpApplicationState Class (Members 3)  Lock and Unlock lock the collection, respectively  Not an ASP topic but a thread synchronization topic  Get returns the value of a collection item  The item can be referenced by string key or ordinal index  Set store a value corresponding to the specified key  The method is thread safe
  • 25. Application State (Best Practices)  Memory to store application state information is permanently allocated  You must write explicit code to release that memory  So don’t try to persist too much application state information  The Cache object, discussed later, provides alternatives to storing application state information
  • 26. Profile Properties  It’s a way to permanently store user-specific data  Data is saved to a programmer-defined data store  It takes quite a bit of programming  The whole thing is unique to windows  We will not use profiles in this course
  • 27. Profiles (Enabling)  Web.config must be modified to enable profiles  You can then reference with code as follows
  • 28. Session State (Introduction)  It is used to preserve state for a user’s ‘session’  Session state works in Web farm or Web garden scenarios  Since the data is persisted in the page  Session data can be stored in databases such as SQL Server or Oracle making it persistent  It’s very powerful and the features have been significantly enhanced in ASP 2.0
  • 29. Session State (Configuration)  The <web.config> file contains a <sessionState> section  The entries in this section configure the state client manager
  • 30. Web.Config <sessionState> <sessionState mode="[Off|InProc|StateServer|SQLServer|Custom]" timeout="number of minutes" cookieName="session identifier cookie name" cookieless= "[true|false|AutoDetect|UseCookies|UseUri|UseDeviceProfile] " regenerateExpiredSessionId="[True|False]" sqlConnectionString="sql connection string" sqlCommandTimeout="number of seconds" allowCustomSqlDatabase="[True|False]" useHostingIdentity="[True|False]" stateConnectionString="tcpip=server:port" stateNetworkTimeout="number of seconds" customProvider="custom provider name"> <providers>...</providers> </sessionState>
  • 31. Session State Providers (1)  Custom – State information is saved in a custom data store  InProc – State information is preserved in the ASP.NET worker process named (aspnet_wp.exe or w3wp.exe)  This is the default option  Off – Session state is disabled
  • 32. Session State Providers (2)  SQLServer – State data is serialized and stored in an SQL server instance  This might be a local or remote SQL Server instance  StateServer – State information is stored in a separate state server process (aspnet_state.exe)  This process can be run locally or on another machine
  • 33. Session State Providers (Best Practices)  Use an SQL server provider to ensure that session state is preserved  Note that there is a performance hit here because the session information is not stored in the page  The InProc provider is not perfect  The ASP worker process might restart thereby affecting session state
  • 34. Session State Identifiers  Browser sessions are identified with a unique identifier stored in the SessionID property  The SessionID is transmitted between the browser and server via  A cookie if cookies are enabled  The URL if cookies are disabled  Set the cookieless attribute to true in the sessionState section of the Web.config file
  • 35. HttpSessionState (Members 1)  CookieMode describes the application’s configuration for cookieless sessions  IsCookieless is used to depict whether cookies are used to persist state  IsNewSession denotes whether the session was created with this request  SessionID gets the unique ID associated with this session  Mode denotes the state client manager being used
  • 36. HttpSessionState (Members 2)  Timeout contains the number of minutes to preserve state between requests  Abandon sets an internal flag to cancel the current session  Add and Remove add or remove an item to the session state  Item reads/writes a session state value  You can use a string key or ordinal index value