Programming Microsoft ASP.NET 4 - Dino Esposito [155]
HttpContext.Current.Profile.GetPropertyValue("UseEuroMetricSystem");
The drawback is that GetPropertyValue is designed to return an Object type. To get a Boolean or a String, you need to cast. The autogenerated profile class you would get in a Web site project just saves you from manually writing a few cast instructions. Here are the steps to take to define a strongly typed profile data model in a WAP project.
The idea is that you define your own strongly typed class and then attach its reference to the ... Here’s a possible implementation for the handmade YourApp.UserProfile wrapper class: namespace YourApp { public class UserProfile : ProfileBase { public static UserProfile GetUserProfile() { var user = Membership.GetUser(); // Anonymous user? if (user == null) return GetUserProfile(""); // throw if anonymous access is not permitted return GetUserProfile(user.UserName); } public static UserProfile GetUserProfile(String username) { var profileFromStorage = Create(username); return profileFromStorage as UserProfile; } [SettingsAllowAnonymous(true)] public Boolean UseEuroMetricSystem { get { return (Boolean) HttpContext.Current.Profile.GetPropertyValue("UseEuroMetricSystem"); } set { HttpContext.Current.Profile.SetPropertyValue("UseEuroMetricSystem", value); } } [SettingsAllowAnonymous(true)] public String TempSystem { get { var current = (String) HttpContext.Current.Profile.GetPropertyValue("TempSystem"); if (String.IsNullOrEmpty(current)) return "F"; return (String) current; } set { HttpContext.Current.Profile.SetPropertyValue("TempSystem", value); } } } } The UserProfile class you see is configured to support both authenticated and anonymous access. If you want to enable it only for authenticated users, throw an exception if no user is found and remove the turn to make the argument of the SettingsAllowAnonymous attribute false. (Or remove the attribute altogether.) To access properties from within the code, you proceed as follows: var profile = Your.UserProfile.GetUserProfile(); if (profile.UseEuroMetricSystem) speedFormat = "{0} kmh"; You invoke the static GetUserProfile method on your wrapper class and get an instance of your own profile class fed by the underlying ASP.NET profile API. The Create method that GetUserProfile uses internally is part of the profile API, and specifically it is the part that communicates with the storage layer. Interacting with the Page Creating the Profile Database ASP.NET 4 (as well as earlier versions) comes with an administrative tool—the ASP.NET Web Site Administration Tool (WSAT)—that is fully integrated in Visual Studio. You invoke the tool by choosing the ASP.NET Configuration item from the Build menu. (See Figure 7-6.)
To enable or disable profile support, you set the enabled attribute of the
As mentioned earlier, profile support works strictly on a per-user basis and is permanently stored in a configured repository. Enabling the feature simply turns any functionality on, but it doesn’t create the needed infrastructure for user membership and data storage. If you intend to use made-to-measure storage (for example, a non–SQL Server database or a SQL Server database with a custom schema of tables), creating any infrastructure is entirely up to you. If you’re OK with the default table and structure, you resort to a free tool integrated in Visual Studio.