简体   繁体   English

无法从Web配置文件访问文件夹路径

[英]Trouble accessing folder path from web config file

I was trying to use the solution found in this post: How to access the folder path in web config using c# but I get a null reference exception (System.Collections.Specialized.NameValueCollection.this[string].get returned null)? 我正在尝试使用本文中找到的解决方案: 如何使用c#访问Web配置中的文件夹路径,但出现空引用异常(System.Collections.Specialized.NameValueCollection.this [string] .get返回null)?

I have this in my web config file (it's inside the configuration like shown in the post linked above): 我在我的Web配置文件中有此文件(它位于上面链接中所显示的配置内部):

<appSettings>
     <add key="SessionTest" value="C:\\Settings\\XmlDir\\Session\\20180824.xml"/>
</appSettings>

and retrieve it like so: 并像这样检索它:

string path = System.Web.Configuration.WebConfigurationManager.AppSettings["SessionTest"].ToString();

Is there something simple that I'm missing here? 我在这里缺少一些简单的东西吗? The file exists in the folder (and I copied the path from the file explorer, so I'm having trouble understanding what is causing the null exception >.<") 该文件位于文件夹中(并且我从文件资源管理器复制了路径,因此我无法理解导致空异常>。<“的原因)

Thanks in advance :) 提前致谢 :)

据我所知,这是访问web.config <appSettings>的标准方法

System.Configuration.ConfigurationManager.AppSettings["SessionTest"]

There was a second web config file that I wasn't aware of (I'm working on an old project so I didn't realize the first team that wrote this had added one to the folder I was working in, which in hindsight is probably a little strange) 还有一个我不知道的第二个Web配置文件(我正在做一个旧项目,所以我没有意识到第一个编写此文件的团队已经在我正在使用的文件夹中添加了一个文件,事后看来是可能有点奇怪)

When I added it to the web config file that is associated with the entire solution and not the one inside the folder I was working in, I was able to retrieve the value successfully! 当我将其添加到与整个解决方案相关联的Web配置文件中,而不是将其添加到与我正在使用的文件夹中的文件相关联的Web配置文件中时,我能够成功检索该值! I want to also note I included SLaks suggestion from the comment he made (which is not to say it doesn't work with the double slashes, but I didn't include them when I found success so I can't confirm that). 我还想指出的是,我在他的评论中包括了SLaks的建议(并不是说它不适用于双斜杠,但是当我发现成功时我没有将它们包括在内,因此我无法确认)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM