Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#无法从MVC中的App.config文件中读取_C#_Asp.net_.net_Git_Model View Controller - Fatal编程技术网

C#无法从MVC中的App.config文件中读取

C#无法从MVC中的App.config文件中读取,c#,asp.net,.net,git,model-view-controller,C#,Asp.net,.net,Git,Model View Controller,我有一个MVC项目,正在尝试将我的API密钥存储在一个单独的配置文件中,当将代码推送到Git时,我将忽略该文件。根据MSDN,我应该能够将它们存储在App.config中,就像这样 <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="APIKey" value="APIKeyValue" /> </appS

我有一个MVC项目,正在尝试将我的API密钥存储在一个单独的配置文件中,当将代码推送到Git时,我将忽略该文件。根据MSDN,我应该能够将它们存储在App.config中,就像这样

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="APIKey" value="APIKeyValue" />
    </appSettings>
</configuration>
然后调用控制器中的方法从App.config文件中分配值(这样我就知道我得到了值)


代码在任何时候都不会因断点而中断,构建将成功,我无法判断是否获得了值。非常感谢您的帮助。谢谢

对于像MVC应用程序这样的web应用程序,如果您想从源代码管理中删除“机密”,那么它是一个
web.config
文件,而不是
app.config
(re:
web.config
vs
app.config
):

web.config中


....
...
然后在一个separte
AppKeys.config
文件中(您可以将其命名为which.config,示例如上所述),您不需要将其添加到
Git
/源代码管理:


...
请注意,
AppKeys.config
没有XML声明


Hth.

即使将appSettings移动到Web中,我也会遇到同样的问题。请使用访问器进行配置<代码>ConfigurationManager.AppSettings[“APIKey”]
  public class KeyTest 
  {
    public string KeyTestCall()
    {
      string testkey = ConfigurationManager.AppSettings.Get("APIKey");
      return testkey;
    }
  }
public void Testing()
    {
        KeyTest k = new KeyTest();
        ViewBag.x = k;      
    }