首先,你需要定义为了告诉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
什么是错误讯息您收到? – Kami
你是否在''中定义了它? –
Coder
yes –