访问web.config属性(不是appSettings)

时间:2011-03-23 10:10:46

标签: asp.net asp.net-mvc-2

鉴于我可以向库存成员资格提供程序添加各种属性,我假设我可以对自己的提供程序实现执行相同的操作。

<add name="MyMembershipProvider" type="Portal.Infrastructure.MyMembershipProvider"
             enablePasswordRetrieval="false"
                enablePasswordReset="true"
                requiresQuestionAndAnswer="true"
                [...] />

我现在的问题:如何在代码中访问这些值?我知道ConfigurationManager可用于访问appSettings部分中的键值对,但这是不同的。

1 个答案:

答案 0 :(得分:2)

我不知道您尝试访问哪个部分,但一般的想法是您可以通过以下方式直接访问WebConfig:

Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);  
// Get the object related to the <identity> section.
IdentitySection section = (IdentitySection)config.GetSection("system.web/identity");

而不是身份部分放置你的部分。