How to prevent duplicate appSettings in web.config
<p>ASP.NET MVC4 C# application web.config contains setting like</p>

<pre><code>&lt;configuration&gt;
  &lt;appSettings&gt;
    &lt;add key="DefaultDataBase"
         value="db1"/&gt;
  ..
</code></pre>

<p>Method</p>

<pre><code>public static string DefaultDataBase
{
    get
    {
        return WebConfigurationManager.AppSettings["DefaultDataBase"] ?? "mydefault";
    }
}
</code></pre>

<p>Is used to get it in code.
If web.config accidently contains duplicate key like</p>

<pre><code>&lt;configuration&gt;
  &lt;appSettings&gt;
    &lt;add key="DefaultDataBase"
         value="db1"/&gt;
    &lt;add key="DefaultDataBase"
         value="db2"/&gt;

  ..
</code></pre>

<p>this code silently resturns wrong value db2</p>

<p>How to detect duplicate keys and throw ApplicationException on duplicates ?</p>