2011-12-29 57 views
1

我一直在关注加载自定义configSection的msdn上的本教程。 http://msdn.microsoft.com/en-us/library/system.configuration.configurationcollectionattribute.aspx我在asp.net这样做的,我一直在Custom ConfigSection return null

System.Configuration.Configuration config = 
       ConfigurationManager.OpenExeConfiguration(
       ConfigurationUserLevel.None) as Configuration; 

得到一个错误,所以我删除了,只用于

UrlsSection myUrlsSection = 
      ConfigurationManager.GetSection("MyUrls") as UrlsSection; 

可能有人复制和过去从MSDN代码,并作出项目在asp.net和测试代码,看看他们是否得到相同的错误,或看看如果myUrlsSection = null,如果你没有得到一个问题,你可以分享你做了什么,使其工作

+0

你可以发布你的web.config条目去除部分并使用它吗?如果可以的话,还可以发布UrlsSection类的声明。 – 2011-12-29 22:42:20

+0

我刚刚复制并粘贴了教程做了什么 – ONYX 2011-12-29 22:47:13

回答

3

这是我试过它的工作很好。

UrlsSection myUrlsSection = 
      ConfigurationManager.GetSection("MyUrls") as UrlsSection; 

UrlsSection

namespace MyProject 
{ 
    public class UrlsSection : ConfigurationSection 
    { 
    } 
} 

而且我的配置项。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="MyUrls" type="MyProject.UrlsSection, MyProject" /> 
    </configSections> 
    <MyUrls /> 
</configuration> 
+0

它工作时,我用

与类型=“MyProject.UrlSection,MyProject”MyProject结束它说找不到指定的程序集或引用,另一件事情,当我使用ConfigurationCollectionAttribute,版本= 1.0.0.0,文化=中性,PublicKeyToken = null它会产生相同的错误 – ONYX 2011-12-29 23:45:35

+0

为什么我不得不使用type =“MyProject.UrlsSection,而不是你是例子type =“MyProject.UrlsSection,MyProject” – ONYX 2011-12-29 23:57:01

+0

因为这是你指定'type =“namespace.classname,assemblyname”'。显然,UrlsSection类不在你的程序集MyProject中它不起作用,它仍然可以位于'MyProject'命名空间中,但不能放在程序集中,这就是为什么你的项目结构与我的不同,就是这样。 – 2011-12-30 00:03:57