C# appSettings 和 ConfigurationManager.AppSettings 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/712068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
appSettings and ConfigurationManager.AppSettings issue
提问by Brandon Watson
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:
我搜索了该站点,虽然我找到了一些非常有用的信息,但我无法弄清楚我的代码发生了什么。我有以下 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>
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.
我已经删掉了 system.web 和 system.webServer 中的内容,但它是在 ASP.NET MVC 应用程序中生成的默认设置。
I am trying to access the keys in the section (this is a simple Facebook application using FB Connect).
我正在尝试访问该部分中的密钥(这是一个使用 FB Connect 的简单 Facebook 应用程序)。
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;
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?
在我的 .cs 文件的顶部。我非常怀疑错误存在于键盘后(即在我的大脑中),但我无法解决这个问题。有任何想法吗?
回答by Michael Kniskern
Have you tried:
你有没有尝试过:
return ConfigurationManager.AppSettings.Get("APIKey");
回答by Mark Sherretta
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".
检查以确保 web.config 文件的构建操作为“无”。如果构建操作是“嵌入式资源”,我已经看到了这个问题。
回答by Mark Brackett
Does your machine.config have the requisite AppSettings section. It should look something like (version numbers would be different):
您的 machine.config 是否具有必需的 AppSettings 部分。它应该看起来像(版本号会有所不同):
<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.Settingslook like?
您确定您使用的是正确的 web.config 吗?是什么ConfigurationManager.AppSettings.Settings样子?
回答by MSIL
I bet u r using the right syntax to access them when you write: ConfigurationManager.AppSettings["Key1"];
我打赌你在编写时使用正确的语法来访问它们: ConfigurationManager.AppSettings["Key1"];
Try this format for the web config
尝试这种格式的网络配置
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="Key1" value="Val1"/>
<add key="Key2" value="Val2"/>
</appSettings>
<connectionStrings>
......
......
</connectionStrings>
<system.web>
<sessionState mode="InProc" timeout="20"/>
<authorization>
......
</authorization>
<pages>
<namespaces>
<add namespace="System.Data"/>
<add namespace="System.Data.SqlClient"/>
<add namespace="System.IO"/>
</namespaces>
</pages>
<customErrors mode="Off"/>
<compilation debug="true"/></system.web>
</configuration>
回答by Zhaph - Ben Duguid
Have you tried using the WebConfigurationManager:
您是否尝试过使用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.
这是在 Web 应用程序中使用配置文件的首选选项 - 它处理诸如嵌套配置文件之类的事情。
回答by robertz
Visual Studio creates 2 web.config files for MVC. 1 at the root, and the other in the Views folder.
Visual Studio 为 MVC 创建 2 个 web.config 文件。1 个位于根目录,另一个位于 Views 文件夹中。
回答by zaheer ahmad
i have two project in my solution first i add app.config file in class library project which all instances is call from console application i added these entries in config file in class lib project
我的解决方案中有两个项目,首先我在类库项目中添加 app.config 文件,所有实例都是从控制台应用程序调用的,我在类库项目的配置文件中添加了这些条目
<appSettings>
<add key="userName" value="user2" />
<add key="emilsLimit" value="50" />
</appSettings>
it was throwing null exception when i get these in a class in class library project but when i delete app.config from class Library project and added in Console project it works.Cheers
当我在类库项目中的类中获取这些时,它抛出空异常但是当我从类库项目中删除 app.config 并添加到控制台项目中时,它可以工作。干杯
Note: Class lib project reference is added in console
注:控制台添加类lib项目引用
回答by mattnedrich
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.
您确定您使用的是正确的 Web.Config 文件吗?您可能希望使用应用程序根目录中的那个,而不是 Views 目录中的那个。