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

后端 未结 3 1701
悲&欢浪女
悲&欢浪女 2021-02-05 21:55

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 cre

3条回答
  •  离开以前
    2021-02-05 22:36

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

    
      
        
      
    
    

    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;
    

提交回复
热议问题