Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing values in the web.config - appSettings or configSection - which is more efficient?

I'm writing a page that can use a couple of different themes, and I'm going to store some information about each theme in the web.config.

Is it more efficient to create a new sectionGroup and store everything together, or just put everything in appSettings?

configSection solution

<configSections>
    <sectionGroup name="SchedulerPage">
        <section name="Providers" type="System.Configuration.NameValueSectionHandler"/>
        <section name="Themes" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
</configSections>
<SchedulerPage>
    <Themes>
        <add key="PI" value="PISchedulerForm"/>
        <add key="UB" value="UBSchedulerForm"/>
    </Themes>
</SchedulerPage>

To access values in the configSection, I am using this code:

    NameValueCollection themes = ConfigurationManager.GetSection("SchedulerPage/Themes") as NameValueCollection;
    String SchedulerTheme = themes["UB"];

appSettings solution

<appSettings>
    <add key="PITheme" value="PISchedulerForm"/>
    <add key="UBTheme" value="UBSchedulerForm"/>
</appSettings>

To access values in appSettings, I am using this code

    String SchedulerTheme = ConfigurationManager.AppSettings["UBSchedulerForm"].ToString();
like image 874
coder1 Avatar asked Oct 15 '08 13:10

coder1


People also ask

What is the use of appSettings in web config?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.

What is the appSettings section in the web config file in asp net?

AppSetting section in the configuration file is a section that allows us to keep configurable and application wide settings (for e.g.: ConnectionString) that an application requires in order to perform the tasks properly. This helps in easy maintenance and deployment of the application.


3 Answers

For more complex configuration setup, I would use a custom configuration section that clearly defines the roles of each section for example

<appMonitoring enabled="true" smtpServer="xxx">
  <alertRecipients>
    <add name="me" email="[email protected]"/>
  </alertRecipient>
</appMonitoring>

In your configuration class you can expose your properties with something like

public class MonitoringConfig : ConfigurationSection
{
  [ConfigurationProperty("smtp", IsRequired = true)]
  public string Smtp
  {
    get { return this["smtp"] as string; }
  }
  public static MonitoringConfig GetConfig()
  {
    return ConfigurationManager.GetSection("appMonitoring") as MonitoringConfig
  }
}

You can then access configuration properties in the following way from your code

string smtp = MonitoringConfig.GetConfig().Smtp;
like image 198
Nick Allen Avatar answered Oct 05 '22 03:10

Nick Allen


There will be no measurable difference in terms of efficiency.

AppSettings is great if all you need are name/value pairs.

For anything more complex, it's worth creating a custom configuration section.

For the example you mention, I'd use appSettings.

like image 34
Joe Avatar answered Oct 05 '22 01:10

Joe


There'll be no difference in performance, since ConfigurationManager.AppSettings just calls GetSection("appSettings") anyway. If you'll need to enumerate all the keys, then a custom section will be nicer than enumerating all of appSettings and looking for some prefix on the keys, but otherwise it's easier to stick to appSettings unless you need something more complex than a NameValueCollection.

like image 22
stevemegson Avatar answered Oct 05 '22 01:10

stevemegson