appSettings and ConfigurationManager.AppSettings issue

后端 未结 8 1546
情书的邮戳
情书的邮戳 2021-02-12 22:36

I have searched the site, and while I found some very useful information, I couldn\'t figure out what is going on with my code. I have the following web.config:



        
相关标签:
8条回答
  • 2021-02-12 23:13

    Are you sure that you're using the correct Web.Config file? You probably want to use the one at the application root, not the one in the Views directory.

    ASP.NET MVC and two Web.config files

    0 讨论(0)
  • 2021-02-12 23:14

    Have you tried using the WebConfigurationManager:

    return System.Web.Configuration.WebConfigurationManager.AppSettings["APIKey"];
    

    This is the preferred option for using config files in a web app - it handles things like nested config files, etc.

    0 讨论(0)
  • 2021-02-12 23:15

    Have you tried:

    return ConfigurationManager.AppSettings.Get("APIKey");
    
    0 讨论(0)
  • 2021-02-12 23:16

    Visual Studio creates 2 web.config files for MVC. 1 at the root, and the other in the Views folder.

    0 讨论(0)
  • 2021-02-12 23:17

    Does your machine.config have the requisite AppSettings section. It should look something like (version numbers would be different):

    <configuration>
       <configSections>
           <section name="appSettings"
              type="System.Configuration.NameValueFileSectionHandler, System,          Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
       </configSections>
    </configuration>
    

    Are you sure you're using the correct web.config? What does ConfigurationManager.AppSettings.Settings look like?

    0 讨论(0)
  • 2021-02-12 23:22

    Check to make sure the Build Action of the web.config file is "None". I have seen this problem if the build action is "Embedded Resource".

    0 讨论(0)
提交回复
热议问题