C# ConfigurationSection 属性为空
C# ConfigurationSection Propeties are empty
大家晚上好,我正在做一个小项目,我写的 ConfigurationSection 没有返回我在 web.config.
中输入的数据
在此处输入代码
我的代码:
public class AdminSection : ConfigurationSection
{
private static AdminSection uniqueInstance;
public static AdminSection Instance
{
get { return uniqueInstance ?? (uniqueInstance = new AdminSection()); }
}
private AdminSection()
{
}
[ConfigurationProperty("Username", IsRequired =true)]
public String Username
{
get { return (String)this["Username"]; }
}
[ConfigurationProperty("Password", IsRequired = false)]
public String Password
{
get { return (String)this["Password"]; }
}
}
这是我的 web.config
<configuration>
<configSections>
<section name="Admin" type="cms.Configs.AdminSection, cms.cms"/>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
<Admin>
<Username>test</Username>
<Password>test2</Password>
</Admin>
</configuration>
当我尝试调用以下内容时
Username.Value == Configs.AdminSection.Instance.Username
我没有从 AdminSection 获得任何值。
你们中有人知道我可能做错了什么吗?
找到遮阳篷我忘了做以下事情:
public 管理员部分管理员
{
get
{
try
{
if (_instance == null)
{
_instance = ConfigurationManager.GetSection("Admin") as AdminSection;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return _instance;
}
}
大家晚上好,我正在做一个小项目,我写的 ConfigurationSection 没有返回我在 web.config.
中输入的数据在此处输入代码
我的代码:
public class AdminSection : ConfigurationSection
{
private static AdminSection uniqueInstance;
public static AdminSection Instance
{
get { return uniqueInstance ?? (uniqueInstance = new AdminSection()); }
}
private AdminSection()
{
}
[ConfigurationProperty("Username", IsRequired =true)]
public String Username
{
get { return (String)this["Username"]; }
}
[ConfigurationProperty("Password", IsRequired = false)]
public String Password
{
get { return (String)this["Password"]; }
}
}
这是我的 web.config
<configuration>
<configSections>
<section name="Admin" type="cms.Configs.AdminSection, cms.cms"/>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
<Admin>
<Username>test</Username>
<Password>test2</Password>
</Admin>
</configuration>
当我尝试调用以下内容时
Username.Value == Configs.AdminSection.Instance.Username
我没有从 AdminSection 获得任何值。
你们中有人知道我可能做错了什么吗?
找到遮阳篷我忘了做以下事情:
public 管理员部分管理员
{
get
{
try
{
if (_instance == null)
{
_instance = ConfigurationManager.GetSection("Admin") as AdminSection;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return _instance;
}
}