gpt4 book ai didi

c# - 如何从 MVC4 中的 web.config 获取字符串值

转载 作者:可可西里 更新时间:2023-11-01 03:03:31 25 4
gpt4 key购买 nike

我想获取一个 logFilePath 值,该值是我通过硬编码提供给 appSettings 的。我试图通过以下方式达到关键值

System.Configuration.Configuration rootWebConfig1 = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
System.Configuration.KeyValueConfigurationElement customSetting = rootWebConfig1.AppSettings.Settings["azureLogUrl"];
string pathValue = customSetting.Value;

但我收到了空引用异常。如何从 web.config 文件中获取值?

最佳答案

用途:

string pathValue = ConfigurationManager.AppSettings["azureLogUrl"];

您不需要将其转换为字符串并检查空值,因为文档指出:

Values read from the appSettings element of the Web.config file are always of type String. If the specified key does not exist in the Web.config file, no error occurs. Instead, an empty string is returned.

关于c# - 如何从 MVC4 中的 web.config 获取字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18483826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com