的appSettings和ConfigurationManager.AppSettings问题 [英] appSettings and ConfigurationManager.AppSettings issue
问题描述
我已经搜查了现场,而我发现了一些非常有用的信息,我无法弄清楚到底是怎么回事我的code。我有以下的web.config:
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:
<?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>
我已经剪断了的System.Web中和system.webServer的东西,但它是在一个ASP.NET MVC应用程序生成的默认设置。
I have snipped out the stuff in system.web and system.webServer, but it's the default settings generated in an ASP.NET MVC app.
我试图访问该段密钥(这是使用FB连接一个简单的Facebook应用程序)。
I am trying to access the keys in the section (this is a simple Facebook application using FB Connect).
在我的code,我有以下行:
In my code, I have the following line:
return ConfigurationManager.AppSettings["APIKey"];
和它返回一个空。我不能完全弄清楚到底是怎么回事。我有必要的:
and it is returning a null. I can't quite figure out what is going on. I have the requisite:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Configuration;
在我的cs文件的顶部。我有一个很强烈的怀疑错误存在键盘后(即在我的大脑),但我解决不了这个之一。任何想法?
at the top of my .cs file. I have a very strong suspicion the error exists post-keyboard (i.e. in my brain), but I can't solve this one. Any ideas?
推荐答案
您是否尝试过使用WebConfigurationManager:
return System.Web.Configuration.WebConfigurationManager.AppSettings["APIKey"];
这是在Web应用程序中使用的配置文件的preferred选项 - 它处理之类的东西嵌套的配置文件等。
This is the preferred option for using config files in a web app - it handles things like nested config files, etc.
这篇关于的appSettings和ConfigurationManager.AppSettings问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!