In many applications, you need to store and use unique information for users. When a user accesses a website, you can use the stored information to display the personalized version of the web application to the user. Personalized applications require a large number of elements: they must use unique user identifiers to store information, identify users when users access them again, and then obtain user information as needed. To simplify the application, you can use the ASP. NET configuration file function to perform all the preceding tasks.
The ASP. NET configuration file function associates information with a single user and stores the information in a persistent format. The configuration file allows you to manage user information without creating and maintaining your own databases. In addition, you can use the ASP. Net configuration file function to use relevant user information by using a strong API that can be accessed from any location in the application.
You can use the configuration file to store any type of objects. The configuration file feature provides a common storage feature that allows you to define and maintain almost any type of data while still using data in a type-safe manner.
1. How ASP. NET configuration files work
To use the configuration file, you must first modify the configuration file of the ASP. NET web application to enable the configuration file. To specify the configuration file provider in the configuration, the provider is the basic class for executing low-level tasks such as storage and retrieval of configuration file data. Available. the configuration file provider included in the. NET Framework (the configuration file data is stored in SQL Server ), you can also create and use your own profile provider as described in the implementation profile provider topic. You can specify the sqlprofileprovider instance to connect to the selected database, or use the default sqlprofileprovider instance that stores the configuration file data on the Local Web server.
You can configure the configuration file function by defining the list of attributes whose values you want to maintain. For example, you may need to store your zip code so that your application can provide region-specific information, such as weather forecasts. In the configuration file, you should define a configuration file attribute named postalcode. The profile section of the configuration file is similar to the following:
<Profile>
<Properties>
<Add name = "postalcode"/>
</Properties>
</Profile>
When an application is running, ASP. NET creates a profilecommon class, which is a dynamically generated class inherited from the profilebase class. The dynamic profilecommon class includes defining the attributes created based on the configuration file attributes specified in the application configuration. The dynamic profilecommon class instance is then set to the profile attribute value of the current httpcontext and can be used on the application page.
In an application, you can collect the values to be stored and assign them to the defined configuration file attributes. For example, the home page of an application may contain a text box prompting the user to enter a zip code. When you enter the zip code, you can set the profile attribute to store the value of the current user, as shown in the following example:
Profile. postalcode = txtpostalcode. text;
When you set a value for profile. postalcode, this value is automatically stored for the current user. You do not need to write any code to determine the identity of the current user or explicitly store the value in the database. This configuration file function will execute these tasks for you.
If you want to use this value, you can use the same method as setting this value to obtain this value. For example, the following code example shows how to call the hypothetical function named getweatherinfo to pass it to the zip code of the current user stored in the configuration file:
Weatherinfo = getweatherinfo (profile. postalcode );
You do not need to explicitly identify a user or perform any database searches. You only need to obtain the attribute value from the configuration file so that ASP. Net can perform the necessary operations to identify the current user and find the value in the persistent configuration file storage area.