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:
1: <profile>
2: ...
3: <properties>
4: <add name="RecentlyViewed" allowAnonymous="false" type="System.Collections.Generic.List`1[Model.Product]" serializeAs="Xml"/>
5: </properties>
6: </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:
1: using System;
2: using System.Collections.Generic;
3:
4: namespace Model
5: {
6: /// <summary>
7: /// This class wraps the Generic List into a class to serialize in Profile provider
8: /// </summary>
9: [Serializable]
10: public class RecentlyViewed : List<Model.Product>
11: {
12: public RecentlyViewed()
13: {
14: }
15: }
16: }
This works beautifully! Now, in web.config, you can do the following:
1: <profile>
2: ...
3: <properties>
4: <add name="RecentlyViewed" allowAnonymous="false" type="Model.RecentlyViewed" serializeAs="Xml"/>
5: </properties>
6: </profile>
No comments:
Post a Comment