Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [153]

By Root 5805 0
the data model you want to use. Then you attach the data model to the page through the configuration file. The layout of the user profile is defined in the web.config file and consists of a list of properties that can take any of the .NET CLR types. The data model is a block of XML data that describes properties and related .NET Framework types.

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 section of the configuration file. The section is itself part of the larger section, which also includes provider information. The section is located under . Here’s an example of a user profile section:

...

All the properties defined through an tag become members of the dynamically created class and are then exposed as part of the HTTP context of each page. The type attribute indicates the type of the property. If no type information is set, the type defaults to System.String. Any valid CLR type is acceptable.

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 element. Only name is mandatory.

Table 7-1. Attributes of the Element

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


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:

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

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

Return Main Page Previous Page Next Page

®Online Book Reader