2012-11-19 79 views
1

檢索自定義設置,我在它(而不是在AppSettings的),它看起來像這樣一些自定義設置web.config如何從web.config中

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

我如何使用服務器地址我的代碼?

我嘗試以下

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

但它不工作。我錯過了什麼?

+0

什麼是錯誤訊息您收到? – Kami

+0

你是否在''中定義了它? – Coder

+0

yes

回答

0
ConfigurationManager.AppSettings("keyname") 

平時對我的作品

+0

它不在appsettings中,它是自定義設置 –

1

你需要轉換的GetSection("ldapSettings")的返回值,因爲它不返回字符串:

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

它說該ldapsettings沒有定義? –

+0

在頂部使用'Imports System.Configuration'和'Imports System.Web.Configuration'。 – Coder

+0

我導入它們,但仍然是相同的錯誤 –

1

首先,你需要定義爲了告訴ASP.NET有什麼樣的特性,像這樣爲你的自定義配置節的類:

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 

然後,你需要稍微改變你的web.config文件。自定義部分的XML佈局看起來應該是這樣,而不是:

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

然後終於,你可以使用下面的行獲得的設置:

Dim Secure As Boolean = ldapSettings.Settings.Secure 

很抱歉的VB.NET,你可以使用這個工具來轉換,如果你需要:http://www.developerfusion.com/tools/convert/csharp-to-vb/

信息主要源自這裏: http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx

1

我發現了一個更簡單的解決

這裏是我做過什麼:

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