Eray Geveci
Eray Geveci

Reputation: 1129

How to retrieve custom settings from web.config

I have a web.config with some Custom Settings in it(not in appsettings) which look like this:

<ldapSettings>
    <add key="server" value="xxxxxx"/>
    <add key="portNumber" value="28400"/>
    <add key="protocolVersion" value="3"/>
    <add key="secure" value="true"/>
</ldapSettings>

How can I use the server address for my code?

I tried following

dim pfad As String
pfad = System.Configuration.ConfigurationManager.GetSection("ldapSettings")
Dim blas As String
blas =pfad["server"]

But it doesn't work. What am I missing?

Upvotes: 1

Views: 3909

Answers (5)

Eray Geveci
Eray Geveci

Reputation: 1129

I found out a much simpler solution

here is what i did:

 Private config As NameValueCollection
 config = DirectCast(ConfigurationManager.GetSection("ldapSettings"), NameValueCollection)
        Dim server As String
        server = config.[Get]("server")

Upvotes: 1

Martin-Brennan
Martin-Brennan

Reputation: 927

First of all, you will need to define a class for your custom configuration section in order to tell ASP.NET what properties it has, like so:

Public Class ldapSettings
    Inherits ConfigurationSection
    Private Shared LSettings As ldapSettings = TryCast(ConfigurationManager.GetSection("ldapSettings"), ldapSettings)

    Public Shared ReadOnly Property Settings() As ldapSettings
        Get
            Return LSettings
        End Get
    End Property

    <ConfigurationProperty("server")>
    Public Property Server() As String
        Get
            Return Me("server")
        End Get
        Set(value As String)
            Me("server") = value
        End Set
    End Property

    <ConfigurationProperty("portNumber")>
    Public Property PortNumber() As String
        Get
            Return Me("portNumber")
        End Get
        Set(value As String)
            Me("portNumber") = value
        End Set
    End Property

    <ConfigurationProperty("protocolVersion")>
    Public Property ProtocolVersion() As String
        Get
            Return Me("protocolVersion")
        End Get
        Set(value As String)
            Me("protocolVersion") = value
        End Set
    End Property

    <ConfigurationProperty("secure")>
    Public Property Secure() As Boolean
        Get
            Return Me("secure")
        End Get
        Set(value As Boolean)
            Me("secure") = value
        End Set
    End Property
End Class

Then, you will need to change your web.config file slightly. The XML layout of the custom section should look like this instead:

  <configSections>
    <section name="ldapSettings" type="Your_Assembly_Name.ldapSettings"/>
  </configSections>
  <ldapSettings
    server="xxxxxx"
    portNumber="28400"
    protocolVersion="3"
    secure="true"
  />

And then finally, you can get a setting using the following line:

Dim Secure As Boolean = ldapSettings.Settings.Secure

Sorry about the VB.NET, you can use this tool to convert if you need to: http://www.developerfusion.com/tools/convert/csharp-to-vb/

Info mainly sourced from here: http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx

Upvotes: 1

Software Engineer
Software Engineer

Reputation: 3956

You need to cast the return value of GetSection("ldapSettings") because it does not return string:

Dim ldap As ldapSettings = CType(ConfigurationManager.GetSection("ldapSettings"), ldapSettings)
Dim server As String = ldapSettings.server

Upvotes: 1

JIbber4568
JIbber4568

Reputation: 839

ConfigurationManager.AppSettings("keyname") 

usually works for me

Upvotes: 0

Related Questions