title: MVC 3 getting values from AppSettings in web.config
body: <p>In normal ASP.NET Web forms sites I would use web.configs "appsettings" to add application setting data to a site. However, I am not able to retrieve setting values this way when using MVC 3.</p>
  
  <p>First off, there are 2 web.config files. One in the root of the site, the second is listed in the Views area. I assume I want to put my appsettings information in the root web.config file, correct? (putting it in the other under views seems to produce an error stating "AppSettings" can only appear once per web application.)</p>
  
  <p>When I try to retrieve it (C#: System.Configuration.ConfigurationManager.AppSettings["SettingName"]) I get a blank or empty/null return value. What am I doing wrong?</p>
  
  <p>I should mention that I am actually retrieving this information in a Class file under the Models area for set specific values for a model using get; set;. Is it possible that I'm not allowed to do this in Models?</p>
  
  <p>In a Controller.cs:</p>
  
  <pre><code>WindowsLiveConnect.ServiceConfiguration WLSC = new WindowsLiveConnect.ServiceConfiguration();
  
  ViewBag.ClientID = SC.ClientID; // This returns empty
  </code></pre>
  
  <p>In web.config</p>
  
  <pre class="lang-xml prettyprint-override"><code>...
  
    &lt;appSettings&gt;
      &lt;add key="webpages:Version" value="1.0.0.0"/&gt;
      &lt;add key="ClientValidationEnabled" value="true"/&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true"/&gt;
  
      &lt;add key="ClientID" value="0000000040062A3F" /&gt;
      &lt;add key="ClientSecret" value="SUPERSECRETPASSWORD" /&gt;
      &lt;add key="RedirectURL" value="http%3A%2F%2Fwww.quilnet.com" /&gt;
    &lt;/appSettings&gt;
  
  ...
  </code></pre>
  
  <p>In the Model.cs file:</p>
  
  <pre><code>        public class ServiceConfiguration
          {
              private string clientid;
              private string clientsecret;
              private string redirecturl;
  
              public string ClientID
              {
  
                  get { return clientid; }
  
                  set
                  {
                      clientid = System.Configuration.ConfigurationManager.AppSettings["ClientID"];
                  }
              }
  
              public string ClientSecret
              {
  
                  get { return clientsecret; }
  
                  set
                  {
                      clientsecret = System.Configuration.ConfigurationManager.AppSettings["ClientSecret"];
                  }
              }
  
              public string RedirectURL
              {
  
                  get { return redirecturl; }
  
                  set
                  {
                      redirecturl = System.Configuration.ConfigurationManager.AppSettings["RedirectURL"];
                  }
              }
          }
  </code></pre>