Reputation: 771
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"];
}
}
}
Upvotes: 38
Views: 106087
Reputation: 1544
I did it this way:
myVar = System.Configuration.ConfigurationManager.AppSettings["ClientID"].ToString();
Upvotes: 8
Reputation: 106
Looking at the code I assume you are using sharepoint provider hosted apps? Best thing to do here in mvc is to ignore the web.config which is on the view level and only use the one in the root of the webapplication.
The other thing I want to mention is that its probably not a good idea to fetch configuration information from the web.config in the actual model. Its better to move it either to : - the constructor of the controller - the factory/repository which returns this model
Upvotes: 1
Reputation: 819
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);
}
}
Upvotes: 66
Reputation: 28046
Are you ever calling set? I'm guessing it never gets called, so the private variable never gets the value from the config.
Try it this way (just retrieve the value in the get, no set needed):
public string ClientSecret
{
get { return System.Configuration.ConfigurationManager.AppSettings["ClientSecret"]; }
}
Upvotes: 35