[ASP.NET] write their own Configuration section of Part 2: own configuration organized as a Group

Following the previous article: "Do not rely appSettings under the eaves, and write their own Configuration section of it ~" After that, you may have to proceed, or own a set value has moved in its own section, but now we have a question, it is this: if it is not the module section, all crowded in the same section is difficult to see, is not easy to find. Well, this article will help you.


Following the previous article: After not to rely under the eaves of the appSettings, write your own Configuration section of it - perhaps you have already started, or own a set value has moved in its own section, but now there is a problem, that is: If you do not block section, all crowded in the same section is difficult to see, it is not easy to find. Well, this article will help you.

If you've seen the ASP.NET Web.config, it should be noted that the default configuration is Group, for example:

That if we can build yourself a Section Group? The answer is definitely yes.

Configuration to be used as a SectionGroup class must be inherited from the System.Configuration namespace (implicit in the component System.Configuration.dll) of ConfigurationSectionGroup class, for example, I can write this:

In the program, to be wrapped in SectionGroup sections, to be presented in SectionGroup property, and the property to ConfigurationSectionGroup.Sections string key (the same declaration ConfigurationProperty) ConfigurationSection to pick the object you can use as a direct Section transition into their objects.

Then, you can put in Web.config:

Here take note, type setting if it is in the Web site project, directly to the type name can be, but if it is a Web application project, along with the namespace will have to together , otherwise ASP.NET will not find this type of information.

Followed by its configuration values ​​set in the Web.config:

Then, build a test ASP.NET Web Form, and fill in the following procedure:

During this program you may notice that this path mySectionGroup / mySection1, this is the rule ASP.NET configuration system, as defined, if you are using Group organize your section, then use the [group_name] / [section_name] of way to value, so ConfigurationManager.GetSection () will be taken to the Section you want. (Note: ConfigurationManager no GetSectionGroup method, so you can only take Section)

Compile and run this program, then you should see this message:

Original: Large column  [ASP.NET] write their own Configuration section of Part 2: will be organized into a Group own configuration


Guess you like

Origin www.cnblogs.com/chinatrump/p/11505154.html
own