appSettings和ConfigurationManager.AppSettings问题

Bra*_*son 17 c# asp.net asp.net-mvc

我搜索了网站,虽然我发现了一些非常有用的信息,但我无法弄清楚我的代码是怎么回事.我有以下web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
  </system.web>

  <system.webServer>
  </system.webServer>

  <appSettings>
    <add key="APIKey" value="23e24c73feed7ca0f6afd876575842de"/>
    <add key="Secret" value="################################"/>
    <add key="Callback" value="http://localhost:55994/"/>
    <add key="Suffix" value="My3Words"/>
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我已经删除了system.web和system.webServer中的内容,但它是ASP.NET MVC应用程序中生成的默认设置.

我试图访问该部分中的键(这是一个使用FB Connect的简单Facebook应用程序).

在我的代码中,我有以下行:

return ConfigurationManager.AppSettings["APIKey"];
Run Code Online (Sandbox Code Playgroud)

它返回null.我无法弄清楚发生了什么.我有必要的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Configuration;
Run Code Online (Sandbox Code Playgroud)

在我的.cs文件的顶部.我有一个非常强烈的怀疑,键盘后(即在我的大脑中)存在错误,但我无法解决这个问题.有任何想法吗?

Zha*_*uid 29

您是否尝试过使用WebConfigurationManager:

return System.Web.Configuration.WebConfigurationManager.AppSettings["APIKey"];
Run Code Online (Sandbox Code Playgroud)

这是在Web应用程序中使用配置文件的首选选项 - 它处理嵌套配置文件等内容.


rob*_*rtz 9

Visual Studio为MVC创建了2个web.config文件.1位于根目录,另一位位于Views文件夹中.


Mic*_*ern -1

你有没有尝试过:

return ConfigurationManager.AppSettings.Get("APIKey");
Run Code Online (Sandbox Code Playgroud)