Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 33381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:54:42+00:00 2026-05-10T13:54:42+00:00

The web applications I develop often require co-dependent configuration settings and there are also

  • 0

The web applications I develop often require co-dependent configuration settings and there are also settings that have to change as we move between each of our environments.

All our settings are currently simple key-value pairs but it would be useful to create custom config sections so that it is obvious when two values need to change together or when the settings need to change for an environment.

What’s the best way to create custom config sections and are there any special considerations to make when retrieving the values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T13:54:43+00:00Added an answer on May 10, 2026 at 1:54 pm

    Using attributes, child config sections and constraints

    There is also the possibility to use attributes which automatically takes care of the plumbing, as well as providing the ability to easily add constraints.

    I here present an example from code I use myself in one of my sites. With a constraint I dictate the maximum amount of disk space any one user is allowed to use.

    MailCenterConfiguration.cs:

    namespace Ani {      public sealed class MailCenterConfiguration : ConfigurationSection     {         [ConfigurationProperty('userDiskSpace', IsRequired = true)]         [IntegerValidator(MinValue = 0, MaxValue = 1000000)]         public int UserDiskSpace         {             get { return (int)base['userDiskSpace']; }             set { base['userDiskSpace'] = value; }         }     } } 

    This is set up in web.config like so

    <configSections>     <!-- Mailcenter configuration file -->     <section name='mailCenter' type='Ani.MailCenterConfiguration' requirePermission='false'/> </configSections> ... <mailCenter userDiskSpace='25000'>     <mail      host='my.hostname.com'      port='366' /> </mailCenter> 

    Child elements

    The child xml element mail is created in the same .cs file as the one above. Here I’ve added constraints on the port. If the port is assigned a value not in this range the runtime will complain when the config is loaded.

    MailCenterConfiguration.cs:

    public sealed class MailCenterConfiguration : ConfigurationSection {     [ConfigurationProperty('mail', IsRequired=true)]     public MailElement Mail     {         get { return (MailElement)base['mail']; }         set { base['mail'] = value; }     }      public class MailElement : ConfigurationElement     {         [ConfigurationProperty('host', IsRequired = true)]         public string Host         {             get { return (string)base['host']; }             set { base['host'] = value; }         }          [ConfigurationProperty('port', IsRequired = true)]         [IntegerValidator(MinValue = 0, MaxValue = 65535)]         public int Port         {             get { return (int)base['port']; }             set { base['port'] = value; }         } 

    Use

    To then use it practically in code, all you have to do is instantiate the MailCenterConfigurationObject, this will automatically read the relevant sections from web.config.

    MailCenterConfiguration.cs

    private static MailCenterConfiguration instance = null; public static MailCenterConfiguration Instance {     get     {         if (instance == null)         {             instance = (MailCenterConfiguration)WebConfigurationManager.GetSection('mailCenter');         }          return instance;     } } 

    AnotherFile.cs

    public void SendMail() {     MailCenterConfiguration conf = MailCenterConfiguration.Instance;     SmtpClient smtpClient = new SmtpClient(conf.Mail.Host, conf.Mail.Port); } 

    Check for validity

    I previously mentioned that the runtime will complain when the configuration is loaded and some data does not comply to the rules you have set up (e.g. in MailCenterConfiguration.cs). I tend to want to know these things as soon as possible when my site fires up. One way to solve this is load the configuration in _Global.asax.cx.Application_Start_ , if the configuration is invalid you will be notified of this with the means of an exception. Your site won’t start and instead you will be presented detailed exception information in the Yellow screen of death.

    Global.asax.cs

    protected void Application_ Start(object sender, EventArgs e) {     MailCenterConfiguration.Instance; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 116k
  • Answers 116k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since this code was originally used with extended stored procedures,… May 11, 2026 at 10:35 pm
  • Editorial Team
    Editorial Team added an answer The GIMP does a pretty good job reading PDF files.… May 11, 2026 at 10:35 pm
  • Editorial Team
    Editorial Team added an answer Suggestions for a combined system. The cache and other things:… May 11, 2026 at 10:35 pm

Related Questions

When I setup IIS6 to develop projects locally I have to enable Integrated Windows
Is it at all possible to leverage Sharepoint UI (e.g. lists editor), to perform
I'm a C# developer. I develop both Windows & Web Applications. I would like
I'm developing an application for an internal customer. One of the requirements is that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.