[ASP.NET] write their own Configuration section of Part 1: Do not rely appSettings under the eaves, and write their own Configuration section of it ~

I think a lot of people write ASP.NET application, would habitually put some of their own area set value into appSettings in Web.config, and then with ConfigurationSettings.AppSettings (1.1) or ConfigurationManager.AppSettings (2.0+ ) to take the set value, even though it is very easy to use right, but if more than one set of words, appSettings block will be filled with a lot of set values, will be divided is not very good points (a bunch of different setting module are mixed in the same area), therefore, if you want people to write in large ASP.NET application, you may have to learn how to design their own section of the Configuration.


I think a lot of people write ASP.NET application, would habitually put some of their own area set value into appSettings in Web.config, and then with ConfigurationSettings.AppSettings (1.1) or ConfigurationManager.AppSettings (2.0+ ) to take the set value, even though it is very easy to use right, but if more than one set of words, appSettings block will be filled with a lot of set values, will be divided is not very good points (a bunch of different setting module are mixed in the same area), therefore, if you want people to write in large ASP.NET application, you may have to learn how to design their own section of the Configuration.

In fact, their own Configuration section will not be too difficult:

  1. Adding a reference to System.Configuration.dll in the project.
  2. Add a class that inherits from ConfigurationSection class.
  3. Establishing property values ​​to be used, and characteristics apply ConfigurationPropertyAttribute, set attributes.
  4. Register your ConfigurationSection class in Web.config.
  5. Use your ConfigurationSection set in Web.config.

The following is a simple procedure that is ConfigurationSection example:

To use all of the Configuration properties must apply ConfigurationPropertyAttribute to set its properties, such as name, default value, whether the necessary fields, and so on.

Then register it in the Web.config configSections section:


  
     


  

Then ready to be used in Web.config.



...

      ContainerControl
    DisplayDefaultActionPanel="true"
    DefaultActionPanelControlUrl="~/Controls/TestDefaultControl.ascx" />


...

Is not it simple?

After a good definition in the program can be used ConfigurationManager.GetSection () to invoke it:

Original: Large column  [ASP.NET] write their own Configuration section of Part 1: Do not rely appSettings under the eaves, and write their own Configuration section of it ~


Guess you like

Origin www.cnblogs.com/chinatrump/p/11512843.html