Search code examples
c#web-configconfigurationsection

C# ConfigurationSection Propeties are empty


Good evening people, im working on a little project and the ConfigurationSection i wrote is not returning the data i put in in the web.config.

enter code here

my code:

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"]; }
    }
}

here is my 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>

When i trie to call the following

Username.Value == Configs.AdminSection.Instance.Username

Im not getting any values from the AdminSection.

Does any of you have a single clue of what i might be doing wrong?


Solution

  • found the awnser i forgot to do the following :

    public AdminSection Admin

    {
        get
        {
            try
            {
                if (_instance == null)
                {
                    _instance = ConfigurationManager.GetSection("Admin") as AdminSection;   
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return _instance;
        }
    }