我正在使用以下代码来获取会话警告的值。但是当我运行代码时,我收到了 Null Reference 的错误。
function pageLoad() {
var millisecTimeOutWarning = "<%= int.Parse(System.Configuration.ConfigurationManager.AppSettings["SessionTimeoutWarning"].ToString()) * 60 * 1000 %>";
alert(millisecTimeOutWarning);
}
您的代码没有任何问题,如果 AppSetting 键SessionTimeoutWarning
存在,它将起作用。
ConfigurationManager.AppSettings
'System.NullReferenceException'
当键不存在时抛出。
请检查您的配置并确保密钥存在。
请记住,这AppSettings
是一个NameValueCollection
返回的对象ConfigurationManager
public static NameValueCollection AppSettings
{
get
{
object section = GetSection("appSettings");
if (!(section is NameValueCollection))
{
// If config is null or not the type we expect, the declaration was changed.
// Treat it as a configuration error.
throw new ConfigurationErrorsException(SR.Config_appsettings_declaration_invalid);
}
return (NameValueCollection)section;
}
}
如果您尝试从 NameValueCollection 中获取 Key 的值,它将返回 null,如果您执行 ToString() 为 null,您将获得 'System.NullReferenceException'
例如,以下代码将返回与您相同的错误。
NameValueCollection AppSettings = new NameValueCollection();
AppSettings.Add("Key1", "1");
Debug.Write(AppSettings["Key2"].ToString());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句