MVC Web Profiling – Extension

Profiling is a simple concept that allows to track visitors on website and personalize the site based on the information available at your disposal. Profiling in .NET is so simple that, all you need to do is run a Command on Visual Studio Command Prompt and then write some 10 lines of configuration and code to enable profiling. Visual Studio SDK/.NET has the ability to create the database(tables) and all other infrastructure required for profiling. To get basic profiling enabled on your application all you would need is SQL Server

The default profile implementation is most intuitive and perfect way to handle profiling for your application. However, there may be scenarios where we will have to find alternates to implementation done in profiling. We can extend the ASP.NET profiling to use a custom provider, while we are allowed to reuse most of the core profile functionalities. Here I’ll walk you through basics of setting up an Profiling infrastructure and then details on customizing profile provider.

Steps to setup Basic Profiling

  1. To begin with, I assume that, you already have created a MVC3/MVC4 Web Project
  2. Add reference to System.Web.Profile. This is MS namespace for profiling.
  3. Create a strongly typed class – this class will have all properties that you wish to profile/save in profiling database
      • We will create a sample class and mark it as serializable.
      • This class will be serialized into an XML and stored in database.
        [Serializable]
        public class User
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Timezone { get; set; }
        }
    
  4. Implement an extension to get/set values from default profile provider
        public class UserProfile : ProfileBase
        {
            public User User
            {
                get { return (User)GetPropertyValue("User"); }
            }
    
            // Gets the profile of a specific user.
            public static UserProfile GetProfile(string userName)
            {
                return (UserProfile)Create(userName);
            }
    
            //Get Profile of current user
            public static UserProfile GetProfile()
            {
                return (UserProfile)HttpContext.Current.Profile;
            }
        }
  5. Create database – Execute following command and .NET automatically creates the database
    C:\Microsot Visual Studio 11.0>aspnet_regsql -A p -S SQLSERVER08 -E
    
  6. Add configurations to enable profiling
     <connectionStrings>
        <add name="sqldatabaseprovider" connectionString="Data Source=MACHINE\SQLSERVER08;Initial Catalog=aspnetdb;Integrated Security=True;" />
      </connectionStrings>
        <anonymousIdentification enabled="true" />
        <profile defaultProvider="ProfileProvider" inherits="ProfilingExtension.Profile.UserProfile">
          <providers>
            <add name="ProfileProvider"
            type="System.Web.Profile.SqlProfileProvider"
            applicationName="myApp"
            connectionStringName="sqldatabaseprovider"/>
          </providers>
        </profile>
     
  7. How to use profile in your code:
            [HttpPost]
            public ActionResult Index(string username)
            {
                //Create an instance of Profile Object
                UserProfile profile = new UserProfile();
    
                //Access custom property
                profile.User.FirstName = username;
                profile.User.Timezone = TimeZone.CurrentTimeZone.StandardName;
    
                //Save profile for current user
                profile.Save();
                return View();
            }
    

Things to Remember:

Profiling can be done for both authorized user and anonymous user. Basically, when profiling for an authorized user, profiling uses the current context to obtain the current user ID, which can be set using ASP.NET forms/Or any authentication mechanism. For anonymous users, profiling is not supported by default, it has to be explicitly enabled. When anonymous identification is enabled profiling creates a unique ID for a user and stores it in a cookie on user machine, which is periodically cleared. This unique ID is used by profiler to identify the anonymous user, every time he or she visits the website, and personalization can be done based on this.

Steps to Extend Profiler

  1. Create a database instance – good to have have most of the schemas as defined by default profile implementation
  2. Implement the Extension for ProfileProvider – MSDN article
  3. Implement the ProfileBase extension – Will provide access to custom profile properties along with default attributes.
  4. Modify the configuration – explicitly mention if the anonymous profiling is enabled and also configure the custom properties added – MSDN Article

We are done !!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s