. Net MVC website configuration file read/write

Source: Internet
Author: User

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 == = = = = 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.