How to use enumerations in Profile?

If you've tried to put an enum type into the ASP.NET Profile, maybe you've noticed that there's a small caveat with specifying its default value. To specify the default value, you need to use the serialized value, using the same serialization the profile is going to use for this property. For non-string types, the default serializer is XML. So if you add this to your profile section:

<add name="FavoriteDay" type="System.DayOfWeek" defaultValue="Saturday"/>


You'll get some weird XML exception when you try to use that profile property. You could specify the default value in XML serialized format, but there's not much point in doing that. It's much easier to change the serialization format for this property to string:

<add name="FavoriteDay" type="System.DayOfWeek" defaultValue="Saturday" serializeAs="String"/>


Thanks to Stefan Schackow for explaining this.

No Comments