ConfigurationManager.AppSettings 变得为空?
我没有意识到:“在单独的类库中有一个 web.config,并且”正在从不同的 Web 应用程序读取 web.config 应用程序设置。
我正在使用 VS2010 目标框架 3.5
我不知道这里出了什么问题,但当我尝试获取 ConfigurationManager.AppSettings["StoreId"];
时,我得到 null
private string _storeid = GetStoreId;
public static string GetStoreId
{
get
{
return ConfigurationManager.AppSettings["StoreId"];
}
}
<强>web.config:
<appSettings>
<add key="StoreId" value="123" />
</appSettings>
I did not realize that: 'have a web.config in a separate class library and' was reading the web.config app setting from different web application.
I am using VS2010 target framework 3.5
I don't know what is wrong here but I am getting null
when I try to get ConfigurationManager.AppSettings["StoreId"];
private string _storeid = GetStoreId;
public static string GetStoreId
{
get
{
return ConfigurationManager.AppSettings["StoreId"];
}
}
web.config:
<appSettings>
<add key="StoreId" value="123" />
</appSettings>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果您进行单元测试,则需要单元测试项目内APP.CONFIG的副本
If you are UNIT TESTING you need A COPY of the APP.CONFIG inside the UNIT TEST PROJECT
更新
您的 CsProj 文件中可以有一个 AfterTargets 来复制配置:
问题
通常的原因是上下文造成的。
原因
当您有一个包含两个项目的解决方案时,如果
App/ Web.Config
位于主项目中,如果正在运行的应用程序的上下文是第二个项目(例如库、单元测试等),则它将无法工作。难题
要从其他项目中的配置读取值(使用
System.Configuration
)您需要将配置文件移动/复制到具有运行上下文的项目中。不幸的是,复制文件破坏了良好编程的基础; OOP、源代码管理、SOLID 等。酷解决方案
一个漂亮的解决方案是在其他项目中添加配置文件快捷方式,这样您只需更新一个文件:
这会最好将
config
文件的内容划分到项目中。优雅地,就像按照答案 #2 共享程序集文件一样: https://stackoverflow.com/a/15319582/495455 但是唉,这是由上下文Update
You can have an AfterTargets in your CsProj file that copies the config:
Problem
The usual cause for this is due to context.
Cause
When you have a solution with two projects, if the
App/Web.Config
is in the main project it wont work if the context of the running application is the second project such as a library, unit test, etc.Conundrum
To read values from the config in other projects (with
System.Configuration
) you'll need to move/copy the config file to the project with the running context. Unfortunately duplicating files defeats the tenants of good programming; OOP, Source Code Management, SOLID, etc.Cool Solution
A nifty solution is adding config file shortcuts in other projects so you only update one file:
It would be nice to divide the contents of
config
files across project's. Elegantly, like Sharing Assembly Files as per answer #2: https://stackoverflow.com/a/15319582/495455 but alas it's by context免责声明;)这篇文章不是为了回答OP,因为为时已晚,但它绝对会对最终访问此页面的读者有所帮助。
我遇到的问题< /strong> :
ConfigurationManager.AppSettings["uName"]
在我的 C# Web api 项目中返回null
。我检查的基本内容:
1) 在代码
ConfigurationManager.AppSettings["uName"]
中,我使用了精确的键 'uName
',就像我在web.config
文件中一样,即
检查我没有错误输入作为
userName< /code> 而不是
uName
等。2) 由于它是一个 Web API 项目,因此它的文件将是
web.config
而不是 < code>app.config ,以及项目的根文件夹中。 [参考图片]。解决方案:
对我有用的解决方案,
更改
ConfigurationManager.AppSettings["uName"]
为WebConfigurationManager.AppSettings["uName"]
并且
确保我
在正确文件中,即。
正确的文件不是视图文件夹中的 web.config
也调试或发布 web.config
Disclaimer ;) This post is not to answer OP as it is too late but definitely it would help the readers who end up to this page.
Problem I faced :
ConfigurationManager.AppSettings["uName"]
returningnull
in my C# web api project.Basic Things I checked for :
1) In code
ConfigurationManager.AppSettings["uName"]
, I was using exact key 'uName
' as I had inweb.config
file,i.e
Checked that I haven't mis typed as
userName
instead ofuName
etc.2) Since it is a Web API project it would have a file as
web.config
instead ofapp.config
, and that too in root folder of your project. [refer the image].Solution :
The solution that worked for me ,
Changed
ConfigurationManager.AppSettings["uName"]
toWebConfigurationManager.AppSettings["uName"]
and
made sure that I had
in the right file ie.
Right file is not web.config in View folder
neither the debug or release web.config
and:
位于 ASP.NET 应用程序的
web.config
文件中,而不是在 Visual Studio 中添加到类库项目中的某些app.config
文件中, 正确的?如果是这种情况,您不可能得到null
。如果您已将其添加到 Visual Studio 中类库项目的app.config
中,那么获取 null 是完全正常的行为。and:
is located in the
web.config
file of your ASP.NET application and not in someapp.config
file you've added to your class library project in Visual Studio, right? You can't be possibly gettingnull
if this is the case. If you've added this to anapp.config
of you class library project in Visual Studio then getting null is perfectly normal behavior.我刚刚得到答案 DLL 是从另一个项目调用的,而不是在 App.config 中有 create.so 条目的项目中调用,应该移动到调用项目配置文件。
例如,我的解决方案中有 2 个项目,一个类库和其他控制台应用程序。我已在控制台应用程序中添加了类库引用。因此,如果我在类库项目中添加 app.config 文件,它会通过 null 异常。当我添加应用程序时它会起作用控制台应用程序中的.config。希望它有效
I just got answer DLL are called from another project not in the project where there are create.so entries in App.config should b move to calling project config file.
For example i have 2 project in my solution one class library and other console application.i have added class library reference in Console application.So if i add app.config file in class library project it through null exception.it works when i added app.config in console application.Hope it works
应用程序设置会加载到 ConfigurationManager.AppSettings 中,但用户设置(在项目属性的属性设置中)不会加载。
App settings are loaded into ConfigurationManager.AppSettings, but User settings (in Properties settings in your project properties) are not.
在 Visual Studio 中,右键单击配置文件,选择“属性”,然后将“复制到输出目录”更改为“始终复制”或“如果较新则复制”。
或者,手动添加以下部分作为 .csproj 文件中元素的子元素(此部分用于文件“App.config”的“始终复制”):
In Visual Studio, right-click on the config file, select Properties, and then change "Copy to Output Directory" to either "Copy always" or "Copy if newer".
Alternatively, manually add the following section as a child of the element in your .csproj file (this one is for "Copy always" for file "App.config"):
我尝试了所有这些解决方案,但没有一个对我有用。我试图使用“web.config”文件。所有内容都被正确命名并且文件位于正确的位置,但它拒绝工作。然后我决定将我的“web.config”文件重命名为“app.config”,就像这样,它起作用了。
因此,如果您遇到“web.config”文件的问题,请务必将其重命名为“app.config”。
I tried all of these solutions but none worked for me. I was attempting to use a 'web.config' file. Everything was named correctly and the files were in the proper location, but it refused to work. I then decided to rename my 'web.config' file to 'app.config' and just like that, it worked.
So if you are having this issue with a 'web.config' file be sure to rename it to 'app.config'.
当我测试类库(.dll)时,这发生在我身上。它们都在同一个项目中,但库的 App.config 具有我需要的设置。我编写的测试应用程序需要这些设置,因为它正在运行该库。
This happened to me when I was testing a Class Library (.dll). They were both in the same project but the App.config for the library had the settings I needed. The App I had written to test needed the settings because it was running the library.
当我从文件资源管理器复制项目并重命名该项目时,我遇到了这个问题。这复制了 Debug 文件夹,并且由于我没有将其设置为“如果较新则复制”,因此它不会覆盖旧的 App.config 文件。
只需删除 Debug 文件夹并重建即可。希望对某人有帮助。
I got this problem as I copied a project from the file explorer and renamed the project. This copied the Debug folder and as I didn't have it set to 'Copy if newer' it didn't overwrite the old App.config file.
Just delete the Debug folder and rebuild. Hope that helps someone.
我同意上面的答案,我想补充几点
你应该确保你没有在之前和之后添加空格:
参见下面的代码:
如果您在 ida: ClientId 之间放置空格,它将不起作用并返回 null
确保您的键值名称正确
您可以尝试 WebConfigurationManager
I agree with above answer and I would like to add few more points
you should make sure you don't put space before and after the :
see code below:
if you put space between ida: ClientId it will not work and will return null
make sure your key value names are correct
you can try WebConfigurationManager
刚才发生在我身上,只是从另一个项目调用它时。
显然,在另一个项目中,引用并未定义为服务引用,而是定义为连接服务。我删除了引用并重新添加了它。
Happened to me just now, only when calling it from another project.
Apparently, at the other project, the reference has not been defined as a Service Reference but rather as a Connected Service. I deleted the reference and added it again.
辅助类(如果不是)。重复此操作可能会导致混乱
有人忘记它存在于两个地方。
helper class if it is not. Duplicating this could cause confusion should
someone forget it exists in two places.