The website contains many content that needs to be configured, such as website information, registration settings, and upload settings. If you need to create a separate table to store data in the database, there is only one record in the table, which will make the database very bloated and the efficiency of frequent database access is also a problem. Saving in the config file is a good choice and has the cache function!
We can write the configuration in the configuration section of web. config.
<Configuration> <configSections> <section name = type = requirePermission =/> <! -- The configuration section can be defined here --> <! -- For more information on Entity Framework configuration, visit http: </configSections>
However, writing a large number of configurations here will also result in a bloated web. config. You can define the configuration section here and save the specific configuration information in other files.
The preceding configuration information is used as an example to describe the ideal structure.
[ConfigurationProperty (, IsRequired = {[{[] = [ConfigurationProperty ({[] =
Ii. Create an element collection class. The content is simple and commented out.
[ConfigurationCollection( KeyValueElementCollection() : KeyValueElement [ { (KeyValueElement) (.Properties.Contains(name)) [name] =
3. Configuration section class.
Class defines the private property KeyValues. Read/write configuration section set, and then define a series of required configuration attributes. The operation is to read and write the elements of KeyValues.
ConfigurationProperty _property = ConfigurationProperty(, (KeyValueElementCollection), [ConfigurationProperty(, Options = { (KeyValueElementCollection) { [_property] = _value = (KeyValues[] != ) .TryParse(KeyValues[].Value, (KeyValues[] == ) KeyValues[] = KeyValueElement() { Key = , Value = KeyValues[].Value = (KeyValues[] == ) KeyValues[ (KeyValues[] == ) KeyValues[] = KeyValueElement() { Key = , Value = KeyValues[].Value = (KeyValues[] == ) KeyValues[ (KeyValues[] == ) KeyValues[] = KeyValueElement() { Key = , Value = KeyValues[].Value = (KeyValues[] == ) KeyValues[ (KeyValues[] == ) KeyValues[] = KeyValueElement() { Key = , Value = KeyValues[].Value = (KeyValues[] == ) KeyValues[ (KeyValues[] == ) KeyValues[] = KeyValueElement() { Key = , Value = KeyValues[].Value = (KeyValues[] == ) KeyValues[ (KeyValues[] == ) KeyValues[] = KeyValueElement() { Key = , Value = KeyValues[].Value =
4. Read configurations.
And how to read the configuration information. The preceding class is easy to read. You only need to use WebConfigurationManager. OpenWebConfiguration (). GetSection () to obtain the instance of the configuration section. You can use the attribute value.
_uploadConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration().GetSection() _maxSize = _fileParth = Hashtable extTable = , _uploadConfig.FileExt);
5. Write Configuration
Similar to reading, after setting the attribute value and saving (), the content will be saved to Config \ Upload. config.
The Code is as follows:
_uploadConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration().GetSection() _maxSize == = = = =