Online Book Reader

Home Category

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

By Root 5644 0
to the pattern App_Code.xxx.N.cs, where xxx is a system-generated hash code and N is a 0-based index. Note that the path of the Temporary ASP.NET Files folder is different if you’re using IIS or the embedded Visual Studio Web server. If you’re using IIS, the path is

%Windows%\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

Otherwise, the path is

C:\Users\...\AppData\Local\Temp\Temporary ASP.NET Files

You can programmatically find out the real path being used by reading the value of the following expression:

HttpRuntime.CodegenDir

You can do that by placing a breakpoint somewhere in the page startup code and evaluating the expression in a Visual Studio QuickWatch window.

Using Collection Types


In the previous example, we worked with single, scalar values. However, the personalization engine fully supports more advanced scenarios, such as using collections or custom types. Let’s tackle collections first. The following code demonstrates a Locations property that is a collection of strings:

type="System.Collections.Specialized.StringCollection" />

Nonscalar values such as collections and arrays must be serialized to fit in a data storage medium. The serializeAs attribute simply specifies how. As mentioned, acceptable values are String, Xml, Binary, and ProviderSpecific. If the serializeAs attribute is not present in the definition, the String type is assumed. A collection is normally serialized as XML or in a binary format.

Using Custom Types


You can use a custom type with the ASP.NET personalization layer as long as you mark it as a serializable type. You simply author a class and compile it down to an assembly. The name of the assembly is added to the type information for the profile property:

type="My.Namespace.DataContainer, MyAssembly"

serializeAs="Binary" />

The assembly that contains the custom type must be available to the ASP.NET application. You obtain this custom type by placing the assembly in the application’s Bin directory or by registering it within the global assembly cache (GAC).

Grouping Properties


The section can also accept the element. The element allows you to group a few related properties as if they are properties of an intermediate object. The following code snippet shows an example of grouping:

...

Two properties have been declared children of the Metrics group. This means that from now on any access to Speed or Temperature passes through the Metrics name, as shown here:

var windSpeedDisplayText = String.Format("{0} {1}",

windSpeed, Profile.Metrics.Speed);

The System.Web.UI.Page class doesn’t feature any Profile property. However, in a Web site project, the build machinery of ASP.NET generates an extra partial class where the Profile property is defined to just return HttpContext.Current.Profile.

Note

Default values are not saved to the persistence layer. Properties declared with a default value make their debut in the storage medium only when the application assigns them a value different from the default one.

Definition of the Data Model in a WAP Project


In a WAP project, you can choose between a weakly typed and strongly typed approach. The simplest approach (but most effective as well?) is the weak typing approach. In this case, you do exactly the same as you would do in a Web site project. The only difference is that you have no Profile property on the Page class and no dynamically built profile class.

As you saw earlier, however, a profile class is not a plain old CLR class—it is expected, instead, to inherit from System.Web.Profile.ProfileBase. The parent class features two generic methods to read and write properties: GetPropertyValue and SetPropertyValue. This is the real code that ultimately retrieves

Return Main Page Previous Page Next Page

®Online Book Reader