首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用WebConfigurationManager.AppSettings检索值

无法使用WebConfigurationManager.AppSettings检索值
EN

Stack Overflow用户
提问于 2013-01-17 07:46:50
回答 2查看 1.3K关注 0票数 1

我在c#中和mvc3一起使用mvc3。我有一个Web.Config设置。

我需要检索<add key="sitelocalization" value="en-GB" />的值。

使用下面的代码,我将收到以下错误:

代码语言:javascript
复制
String reference not set to an instance of a String.
Parameter name: name

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: name

在这一行中,Thread.CurrentThread.CurrentUICulture =新CultureInfo(本地化);

代码语言:javascript
复制
<appSettings>
    <add key="webpages:Enabled" value="false" />
    <add key="sitelocalization" value="en-GB" />
</appSettings>


        string localization = WebConfigurationManager.AppSettings["sitelocalization"];
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(localization);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(localization);
EN

回答 2

Stack Overflow用户

发布于 2013-01-17 07:55:28

尝试使用System.Configuration.ConfigurationManager.AppSettings"sitelocalization“

票数 1
EN

Stack Overflow用户

发布于 2013-01-17 07:59:26

代码语言:javascript
复制
System.Configuration.Configuration rootWebConfig1 =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
            if (rootWebConfig1.AppSettings.Settings.Count > 0)
            {
                System.Configuration.KeyValueConfigurationElement customSetting = 
                    rootWebConfig1.AppSettings.Settings["customsetting1"];
                if (customSetting != null)
                    Console.WriteLine("customsetting1 application string = \"{0}\"", 
                        customSetting.Value);
                else
                    Console.WriteLine("No customsetting1 application string");
            }




<appSettings>
  <add key="customsetting1" value="Some text here"/>
</appSettings>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14374344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档