【问题标题】:How to retrieve custom settings from web.config如何从 web.config 中检索自定义设置
【发布时间】:2012-11-19 10:53:18
【问题描述】:

我有一个web.config,其中包含一些自定义设置(不在 appsettings 中),如下所示:

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

但它不起作用。我错过了什么?

【问题讨论】:

  • 您收到的错误信息是什么?
  • 你在&lt;configSections&gt;里面定义了吗?

标签: asp.net vb.net visual-studio-2008 web-config


【解决方案1】:

你需要转换GetSection("ldapSettings")的返回值,因为它不返回字符串:

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

【讨论】:

  • 它说 ldapsettings 没有定义?
  • 在顶部使用Imports System.ConfigurationImports System.Web.Configuration
  • 您必须为您的自定义部分创建 ldapSettings 部分处理程序类,例如 this
【解决方案2】:

首先,您需要为自定义配置部分定义一个类,以便告诉 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

【讨论】:

    【解决方案3】:

    我发现了一个更简单的解决方案

    这就是我所做的:

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

    【讨论】:

      【解决方案4】:
      ConfigurationManager.AppSettings("keyname") 
      

      通常对我有用

      【讨论】:

      • 它不在应用设置中,这是一个自定义设置
      【解决方案5】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 2015-02-23
      • 2014-10-21
      • 2019-02-20
      相关资源
      最近更新 更多