Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 getting values from AppSettings in web.config

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.

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.)

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?

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?

In a Controller.cs:

WindowsLiveConnect.ServiceConfiguration WLSC = new WindowsLiveConnect.ServiceConfiguration();  ViewBag.ClientID = SC.ClientID; // This returns empty 

In web.config

...    <appSettings>     <add key="webpages:Version" value="1.0.0.0"/>     <add key="ClientValidationEnabled" value="true"/>     <add key="UnobtrusiveJavaScriptEnabled" value="true"/>      <add key="ClientID" value="0000000040062A3F" />     <add key="ClientSecret" value="SUPERSECRETPASSWORD" />     <add key="RedirectURL" value="http%3A%2F%2Fwww.quilnet.com" />   </appSettings>  ... 

In the Model.cs file:

        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"];                 }             }         } 
like image 666
Vinod Kumar Avatar asked Oct 25 '13 17:10

Vinod Kumar


1 Answers

Usually I'm using AppSettings static class to access those parameters. Something like this:

public static class AppSettings  {      public static string ClientSecret     {         get         {             return Setting<string>("ClientSecret");         }     }      private static T Setting<T>(string name)     {         string value = ConfigurationManager.AppSettings[name];          if (value == null)         {             throw new Exception(String.Format("Could not find setting '{0}',", name));         }          return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);     } } 
like image 57
Stanislav Nedeljkovic Avatar answered Sep 26 '22 03:09

Stanislav Nedeljkovic