Asp.net custom settings file




















While working with the. NET application we come across two very common configuration files ie. But appSetting tag allows us to add only key value pair. But there are many scenarios where we want our settings to be something more than just key value pairs. To overcome this situation, Microsoft provides many classes, which allows us to write our custom configuration section. All the classes related to these are present in System.

Configuration dll. In this sample Application, I will show how to create your custom configuration section Group, configuration section, configuration element. In the later part of this sample, I will be showing how to add intellisence to our custom tags in Visual Studio. First of all, we will create a class that represents the innermost element of the custom section.

In our case, it is company tag. To create a configuration element, we need to inherit it from ConfigurationElement class. Now, we need to define the attributes of the configuration element. For this, we will create public properties and add a ConfigurationPropertyAttribute to it. The screenshot is given below of the CompanyElement class.

Now, our Company Element is ready. We need to create a collection of type Company Element. To achieve this, we need to create a class CompanyElementCollection, which inherits from ConfigurationElementCollection, which is an abstract class, so we need to provide the implementation of its two abstract methods and will add ConfigurationCollection attribute to it.

If necessary, you can include versioning information in the type specifier. The whole point of this exercise is to give your application access to the information that was entered into the Web. To do that, you need to create an instance of your custom configuration class and initialize it with the data from the configuration file. By inheriting the ConfigurationSection class and using ConfigurationProperty attributes, you make your custom configuration class conform to the ASP.

NET configuration system. Consequently, you can let the WebConfigurationManager deserialize the configuration elements from Web. That one line gives you an instance of your configuration class with all of the properties initialized from Web. For example, you could write code to retrieve the connection string name:. In case you are wondering, yes you can create multiple sections in the Web. You would have to create multiple declarations in the configSections element.

Each declaration would use the same type specifier, but it would reference different section names. Still with me? This next part gets a little more complicated, but it builds on what you just learned. So far, I have a basic custom section that tells my lockout service which database connection string to use.

If the lockout service only supported specific actions, I could create custom elements and add them to the configuration like this:. The property definitions for these elements in my configuration section class would then look like this:. NET can use reflection to determine that the payment and login elements map to the ActionElement class, because that is the type I used for my properties. However, this time the class inherits from the ConfigurationElement base class, and the attributes are a little fancier than the simple Connection property I showed you earlier.

This approach of using individual configuration elements to define the configuration of the payment and login actions works fine, as long as those are the only actions I want to support. If you recall, my requirements were to support any number of actions, not just payment and login. For that, I need an element collection instead. The ASP. NET configuration system does support collections of elements, but you have to use specific syntax in the configuration file to do it.

The configuration system uses the "clear," "add," and "remove" keywords to work with items in the collection, which should be pretty familiar to you if you have ever used the appSettings or connectionStrings sections.

Those sections are essentially custom configuration sections that contain a collection of custom configuration elements. The first thing I need to do is modify my ActionElement class to include the name property, since the element name itself no longer provides that information.

Notice that the ConfigurationProperty attribute includes the IsKey parameter. I set it to true because I want to use the action name as the key of the collection. That will allow me to retrieve a specific item from the collection by name at run time. Next I need a collection class to hold my ActionElement items. As you might have guessed, the configuration namespace includes a ConfigurationElementCollection base class. Your custom configuration element collection classes inherit from this class.

Okay, that looks like a lot of code, but really, most of what you are doing is overriding the properties and methods in the base class in order to provide the correct type definitions for each operation. The last thing I need to do is update my configuration section class to include a reference to the collection. NET what kind of object to instantiate for the property.

The content you requested has been removed. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:. Archived Forums. Web Forms. Sign in to vote.



0コメント

  • 1000 / 1000