Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Management
Agenda
View state
Application cache
Session state
Profiles
Cookies
View State
Mechanism for persisting relatively
small pieces of data across postbacks
Used by pages and controls to persist
state
Also available to you for persisting state
System.Web.UI.LosFormatter
performs serialization and
deserialization
Optimized for compact storage of strings,
integers, booleans, arrays, and hash
Application Cache
Intelligent in-memory data store
Item prioritization and automatic eviction
Time-based expiration and cache
dependencies
Cache removal callbacks
Cache.Insert
public void Insert (
string key,
// Key that identifies item
object value,
// The item itself
CacheDependency dependencies, // Cache dependencies (if any)
DateTime absoluteExpiration, // When should item expire (absolute)?
TimeSpan slidingExpiration,
// When should item expire (sliding)?
CacheItemPriority priority,
// Item's priority relative to other items
CacheItemRemovedCallback onRemoveCallback // Removal callback delegate
);
Temporal Expiration
Cache.Insert ("Stocks", stocks, null,
DateTime.Now.AddMinutes (5), Cache.NoSlidingExpiration);
Cache Dependencies
Cache.Insert ("Stocks", stocks,
new CacheDependency (Server.MapPath ("Stocks.xml")));
Application Cache
Session State
Read/write per-user data store
Accessed through Session property
Page.Session - ASPX
HttpApplication.Session - Global.asax
Cookied or cookieless
Web Server
ASP.NET
ASP.NET
Session
SessionState
State
Web Server
ASP.NET
ASP.NET
State Server
aspnet_state
aspnet_state
Process
Process
ASP.NET state
service
(aspnet_state.exe)
Web Server
ASP.NET
ASP.NET
Database
Server
ASPState
ASPState
Database
Database
Created with
InstallSqlState.
sql or
InstallPersistSql
State.sql
Session Events
Session_Start event signals new
session
Session_End event signals end of
session
void Session_Start ()
{ Process with handlers in Global.asax
// Create a shopping cart and store it in session state
// each time a new session is started
Session["Cart"] = new ShoppingCart ();
}
void Session_End ()
{
// Do any cleanup here when session ends
}
Session Time-Outs
Sessions end when predetermined
time period elapses without any
requests from session's owner
Default time-out = 20 minutes
Time-out can be changed in
Web.config
Profile Service
Stores per-user data persistently
Strongly typed access (unlike session
state)
On-demand lookup (unlike session state)
Long-lived (unlike session state)
Supports authenticated and anonymous
users
Profile Schema
Profiles
HttpProfileBase
HttpProfileBase
HttpProfile
HttpProfile(Autogenerated
(Autogenerated
HttpProfileBase-Derivative)
HttpProfileBase-Derivative)
HttpProfile
HttpProfile(Autogenerated
(Autogenerated
HttpProfileBase-Derivative)
HttpProfileBase-Derivative)
Profile Providers
AccessProfileProvider
AccessProfileProvider SqlProfileProvider
SqlProfileProvider
Other
OtherProviders
Providers
Profile Data
Stores
Access
SQL Server
Other
Data Stores
Defining a Profile
<configuration>
<system.web>
<profile>
<properties>
<add name="ScreenName" />
<add name="Posts" type="System.Int32" defaultValue="0" />
<add name="LastPost" type="System.DateTime" />
</properties>
</profile>
</system.web>
</configuration>
Using a Profile
// Increment the current user's post count
Profile.Posts = Profile.Posts + 1;
// Update the current user's last post date
Profile.LastPost = DateTime.Now;
Autogenerated class
derived
from HttpProfileBase
Profile property
included in
autogenerated page
class
Profile Groups
Properties can be grouped
<group> element defines groups
<profile>
<properties>
<add ... />
...
<group name="...">
<add ... />
...
</group>
</properties>
</profile>
Profile Providers
Profile service is provider-based
Beta 1 ships with two providers
AccessProfileProvider (Access)*
SqlProfileProvider (SQL Server)
Profiles
Cookies
Mechanism for persisting textual data
Described in RFC 2109
For relatively small pieces of data
HttpCookie Properties
Name
Description
Name
Value
Values
HasKeys
Domain
Expires
Secure
Path
Creating a Cookie
HttpCookie cookie = new HttpCookie ("UserName", "Jeffpro");
Response.Cookies.Add (cookie);
Cookie
name
Cookie
value
Reading a Cookie
HttpCookie cookie = Request.Cookies["UserName"];
if (cookie != null) {
string username = cookie.Value; // "Jeffpro"
...
}