web.config中的自定义配置 [英] Custom configuration in web.config

查看:96
本文介绍了web.config中的自定义配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Web应用程序中实现自定义配置.
在我的Web配置中,我添加了以下部分:

<configsections>
    <section name="SecurityConfiguration" type="EncryptStringTesting.CustomConfiguration, EncryptStringTesting" /> 
  </configsections>  
  <securityconfiguration>
      <stringencryptdecryptpassword value="" />
 </securityconfiguration>



我使用以下代码添加了新类CustomConfiguration:

Inherits System.Configuration.ConfigurationSection
#Region " Properties "
    <configurationproperty("value",> _
  Public Property value() As String
        Get
            Return CStr(Me("value"))
        End Get
        Set(ByVal value As String)
            Me("value") = value
        End Set
    End Property
    <configurationproperty("stringencryptdecryptpassword",>                          DefaultValue:="", _
                          IsRequired:=True, IsKey:=True)> _
   Public Property StringEncryptDecryptPassword() As String
        Get
            Return CStr(Me("StringEncryptDecryptPassword"))
        End Get
        Set(ByVal value As String)
            Me("StringEncryptDecryptPassword") = value
        End Set
    End Property



在我的网页中,我尝试检索自定义值:

Dim config As New CustomConfiguration
        config = System.Configuration.ConfigurationManager.GetSection("SecurityConfiguration")
    
        Response.Write(config.StringEncryptDecryptPassword.ToString)


当我调用System.Configuration.ConfigurationManager.GetSection("SecurityConfiguration")时一无所获.您能告诉我为什么吗?

解决方案

名称不区分大小写吗?您在后面的代码中指定了"SecurityConfiguration",但在xml中将其称为"securityconfiguration". />
顺便说一句,为什么您需要value()属性?


I am trying to implement custom configuration in my web apps.
In my web config I have added the following sections:

<configsections>
    <section name="SecurityConfiguration" type="EncryptStringTesting.CustomConfiguration, EncryptStringTesting" /> 
  </configsections>  
  <securityconfiguration>
      <stringencryptdecryptpassword value="" />
 </securityconfiguration>



I''ve add a new class CustomConfiguration with following code:

Inherits System.Configuration.ConfigurationSection
#Region " Properties "
    <configurationproperty("value",> _
  Public Property value() As String
        Get
            Return CStr(Me("value"))
        End Get
        Set(ByVal value As String)
            Me("value") = value
        End Set
    End Property
    <configurationproperty("stringencryptdecryptpassword",>                          DefaultValue:="", _
                          IsRequired:=True, IsKey:=True)> _
   Public Property StringEncryptDecryptPassword() As String
        Get
            Return CStr(Me("StringEncryptDecryptPassword"))
        End Get
        Set(ByVal value As String)
            Me("StringEncryptDecryptPassword") = value
        End Set
    End Property



In my web page I am try to retrive my custom values:

Dim config As New CustomConfiguration
        config = System.Configuration.ConfigurationManager.GetSection("SecurityConfiguration")
    
        Response.Write(config.StringEncryptDecryptPassword.ToString)


When I call System.Configuration.ConfigurationManager.GetSection("SecurityConfiguration") getting nothing. Could you tell me why?

解决方案

Aren''t the names case sensitive? You specify "SecurityConfiguration" in the codebehind, but you called it "securityconfiguration" in the xml.


Try putting the code in a try/catch block and see what exception is raised.

BTW, why do you need the value() property?


这篇关于web.config中的自定义配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆