Programming Microsoft ASP.NET 4 - Dino Esposito [153]
The simplest way to add properties to the profile storage medium is through name/value pairs. You define each pair by adding a new property tag to the ... All the properties defined through an So in the preceding code snippet, we’re defining a profile class made of two properties. The profile pseudoclass we have in mind looks like the one shown here: class PseudoProfile { public Boolean UseEuroMetricSystem {get; set;} public String TemperatureSystem {get; set;} } Table 7-1 lists the valid attributes for the Table 7-1. Attributes of the Attribute Description allowAnonymous Allows storing values for anonymous users. It is false by default. customProviderData Contains specific data to feed a custom profile provider, if any. defaultValue Indicates the default value of the property. name Name of the property. provider Name of the provider to use to read and write the property. readOnly Specifies whether the property value is read-only. It is false by default. serializeAs Indicates how to serialize the value of the property. Possible values are Xml, Binary, String, and ProviderSpecific. type The .NET Framework type of the property. It is a string object by default. The User Profile Class Representation using System; using System.Web; using System.Web.Profile; public class ProfileCommon : System.Web.Profile.ProfileBase { public virtual bool UseEuroMetricSystem { get { return ((bool)(this.GetPropertyValue("UseEuroMetricSystem"))); } set { this.SetPropertyValue("UseEuroMetricSystem", value); } } public virtual string TempSystem { get { return ((string)(this.GetPropertyValue("TempSystem"))); } set { this.SetPropertyValue("TempSystem", value); } } public virtual ProfileCommon GetProfile(string username) { return ((ProfileCommon)(ProfileBase.Create(username))); } } This code is an excerpt from the real source code created by ASP.NET while compiling the content of the web.config file’s An instance of this class is associated with the Profile property of the HTTP context class and is accessed programmatically as follows: // Use the UseEuroMetricSystem property to determine how to render the page if (HttpContext.Profile.UseEuroMetricSystem) { ... } There’s a tight relationship between user accounts and profile information. We’ll investigate this in a moment—for now, you need to take note of this because anonymous users are supported as well. Note You can retrieve the hidden source code of the profile class (and other internal files) in the Temporary ASP.NET Files folder. The profile class in particular is located in a file named according
There’s no class like PseudoProfile anywhere in the application’s AppDomain; yet the declared data model is dynamically compiled to a class for strongly typed programmatic access. The following code snippet gives you a much clearer idea of the class being generated by ASP.NET out of the profile’s data model: