Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net Web API : adding appSettings

I am migrating a web service from WCF service to ASP.Net Web API. When I try to add the following section to web.config, I get a page with error "Internal Server Error" when I try to run the application locally

<appSettings>
    <add key="Environment" value="production" />
    <add key="CacheEnabled" value="true" />
  </appSettings>

When I remove this section, the error goes away. Can you please tell me how to add appSettings to a Web API project?

like image 639
Aadith Ramia Avatar asked Sep 12 '13 11:09

Aadith Ramia


2 Answers

The following can be a guide on how to do it, verify that your appSettings are inside configuration

<configuration>
  <appSettings>
    <add key="Key" value="Value"/>
  </appSettings>
</configuration>

To get the values of the key from the application you can do

textBox1.Text = ConfigurationManager.AppSettings["Name"];
like image 70
jandresrodriguez Avatar answered Oct 17 '22 16:10

jandresrodriguez


Your appSettings needs to to into the Web.config under the configuration element as per the MSDN docs:

<configuration>
      <appSettings />
</configuration>
  1. Make sure it's in the right element
  2. Make sure there isn't already an existing appSettings tag

Otherwise, you would need to pass on the exact error message.

like image 26
CodingIntrigue Avatar answered Oct 17 '22 18:10

CodingIntrigue