问题
Using asp.net profiles, I've stored a complex type (class) and retrieved it. But it is returning a new object that is not initialized instead of null? Is this the expected behavior, if so how can I determine if I've saved data for the given user?
should be some easy points for someone to pick up..
回答1:
Your question is a little unclear, but I think you're asking why there's a non-null profile data object for a user who you haven't stored data for yet?
This article might hopefully clear it up for you. Some of the relevant bits:
A user profile is a collection of values that the ASP.NET 2.0 runtime groups as public fields of a dynamically generated class. The class is derived from a system-provided class and is extended with the addition of a few new members. The class doesn't have to be marked as Serializable, however its contents are persisted to the storage medium as individual properties. The storage occurs on a per-user basis and is preserved until an administrator deletes it.
And further down:
When the application runs and a page is displayed, ASP.NET dynamically creates a profile object that contains properly typed data and assigns the current settings for the logged user to the properties defined in the data model. The profile object is added to the current HttpContext object and made available to pages through the Profile property. Assuming that the profile model has been defined to store a list of links as a collection, the following code snippet shows how to retrieve the list of favorite links that are created by a given user:
...
This code assumes a Links property in the profile object that references a collection type. When the page is loaded, Links and other properties are initialized to contain the most recently stored values; when the page is unloaded their current contents are stored to the persistent medium.
If you need to track whether a user has ever set profile data before, you might be able to use the FindProfilesByUserName function to check to see if a profile exists before you log them in.
来源:https://stackoverflow.com/questions/3169164/profiles-content-check-if-set