Storing Generics in ASP.NET Profile object

Sometimes, I’ll come across a problem that I research for an hour or two. When I find the solution, I think “Wow, that should have been the first thing I tried!”
This is one of those occasions.

I decided to use the ASP.NET profile instead of Session objects to store a list of products. So, in web.config, I tried a number of “work-arounds” that I found online, including:

  <profile>
  ...
        <properties>
          <add name="RecentlyViewed" allowAnonymous="false" type="System.Collections.Generic.List`1[Model.Product]" serializeAs="Xml"/>
        </properties>
  </profile>

The problem I was having, and it seems a lot of people are having, is that System.Collections.Generic.List`1[Model.Product] throws an error. For some people, it seems to work. Well, I tried changing the lt and gt in the generic list so the XML would parse it as it is written in the code-behind. That didnt work.
The solution: Create a class which inherits from

List<Model.Product>

For instance:

 using System;
 using System.Collections.Generic;
  
 namespace Model
 {
     /// <summary>
     /// This class wraps the Generic List into a class to serialize in Profile provider
     /// </summary>
     [Serializable]
      public class RecentlyViewed : List<Model.Product>
      {
          public RecentlyViewed()
          {
          }
      }
  }
 

This works beautifully! Now, in web.config, you can do the following:

  <profile>
  ...
        <properties>
          <add name="RecentlyViewed" allowAnonymous="false" type="Model.RecentlyViewed" serializeAs="Xml"/>
        </properties>
  </profile>