03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

648 x CHAPTER 17 PERSONALIZING WEBSITES<br />

<br />

The first part of the type attribute looks quite normal. The List class lives <strong>in</strong> the System<br />

.Collections.Generic namespace so it makes sense that you need to specify that here as well.<br />

However, right after the class name (List) you see `1. This is not a typo, but the .<strong>NET</strong> way to refer<br />

to generic types <strong>in</strong> pla<strong>in</strong> text. To def<strong>in</strong>e a property that is based on a generic type, you need to use<br />

the back tick (`) followed by a 1. The back tick is usually found to the left of the 1 key on your keyboard.<br />

The `1 is then followed by a pair of square brackets that conta<strong>in</strong>s the actual type you want<br />

to use for the list. The type specified <strong>in</strong> the FavoriteGenres profile property maps to these <strong>VB</strong>.<strong>NET</strong><br />

<strong>and</strong> C# counterparts:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Dim FavoriteGenres As New List(Of Integer)<br />

C#<br />

List FavoriteGenres = new List();<br />

You see how to make use of this <strong>and</strong> other profile properties <strong>in</strong> the follow<strong>in</strong>g exercises. First, you<br />

learn how to configure Profile <strong>in</strong> Web.config <strong>in</strong> the next Try It Out. Later exercises show you how<br />

to work with these properties, <strong>and</strong> how to use the various methods of the List class.<br />

TRY IT OUT<br />

Creat<strong>in</strong>g a Profile<br />

In this Try It Out, you see how to create a profile that is capable of stor<strong>in</strong>g a user’s first <strong>and</strong> last name,<br />

a date of birth, a short biography, <strong>and</strong> a list of IDs of the user’s favorite genres. This list is later used to<br />

show only the reviews that match the user’s <strong>in</strong>terest.<br />

1. Open the Web.config file from the root of the site <strong>and</strong> locate the element that was<br />

added by NuGet <strong>in</strong> an exercise <strong>in</strong> the preced<strong>in</strong>g chapter. If you don’t have this element yet, refer to<br />

the section “Introduc<strong>in</strong>g the Log<strong>in</strong> Controls” <strong>in</strong> Chapter 16 to learn how to configure your site for<br />

the application services.<br />

2. Add a new element as a direct child of . Make sure you don’t accidentally<br />

add the new element <strong>in</strong>side the element.<br />

3. Complete the element so it ends up look<strong>in</strong>g like this:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

...<br />

<br />

4. Save the Web.config. As soon as you save the file, a background process starts to generate a class<br />

file that is used for the profile. After the class file has been created <strong>and</strong> compiled successfully, you<br />

can access it programmatically through the Profile property of the Page class.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!