Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you need can be achieved using <strong>Custom Configuration Sections</strong>. This <a href="http://msdn.microsoft.com/en-us/library/2tw134k3.aspx" rel="nofollow">MSDN article</a> provides some details and samples on how to implement such config sections.</p> <p>If you need simple <strong>key/value</strong> configs, like in the appSettings section, then creating a custom configuration section is quite simple.</p> <p>The first thing you need to do is define your <strong>config section</strong> inside web.config:</p> <pre><code>&lt;configSections&gt; &lt;section name="GmailSettings" restartOnExternalChanges="true" type="System.Configuration.NameValueFileSectionHandler" /&gt; &lt;/configSections&gt; </code></pre> <p>Now inside the web.config file you can <strong>declare</strong> the GmailSettings section and set the location of the external config file that you are going to use for that setting:</p> <pre><code>&lt;GmailSettings configSource="GmailSettings.config"&gt;&lt;/GmailSettings&gt; </code></pre> <p><strong>configSource</strong> specifies the name and location of the external config file used for defining the GmailSettings section. Please note the <code>restartOnExternalChanges</code> attribute used when defining the section. Set it to true if you want the application to be automatically restarted when you modify the GmailSettings.config file (as it does when you modify the web.config file).</p> <p>Below is an example of how to implement the <strong>GmailSettings.config</strong> file:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;GmailSettings&gt; &lt;add key="userName" value="blabla"/&gt; &lt;add key="password" value="moreBla"/&gt; &lt;/GmailSettings&gt; </code></pre> <p>You can access the settings from GmailSettings using the <code>ConfigurationManager.GetSection()</code> method or by implementing a <strong>helper class</strong> like the one below:</p> <pre><code>public class GmailSettings { private static readonly GmailSettings _instance = new GmailSettings(); private NameValueCollection _settings = ConfigurationManager.GetSection("GmailSettings") as NameValueCollection; public static GmailSettings Instance { get { return _instance; } } public string this[string key] { get { return _settings[key]; } } } </code></pre> <p>The settings can now be accessed like <code>GmailSettings.Instance["userName"]</code>.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload