无法使用WebConfigurationManager.AppSettings检索值

时间:2013-01-17 07:46:50

标签: c# asp.net web-config

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

我需要检索<add key="sitelocalization" value="en-GB" /&gt;。

的值

使用以下代码我收到此错误:

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 = new CultureInfo(本地化);


<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);

2 个答案:

答案 0 :(得分:1)

尝试使用System.Configuration.ConfigurationManager.AppSettings [&#34; sitelocalization&#34;]

答案 1 :(得分:0)

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>