我按照书的测试一下如何读取config.web中的内容,但出错。
config.web中的内容
++++++++++++++++++++++++++++++
<configuration> 
  <system.web>
<compilation debug="true"/>
 </system.web>
<configsections> 
<add name="test" type="System.Web.Configuration.DictionarySectionHandler" />
</configsections> 
<test>
 <add key="key" value="just a configure test" />
</test> 
</configuration>
+++++++++++++++++++++++++++
读取页面的内容
+++++++++++++++++++++++++
<%@ Page Language="vb" Debug="true" %>
<script runat="server">
sub page_load(s as object ,e as eventargs) 
Dim CfgSection As Hashtable = Context.GetConfig("test") 
Dim Msg As String = cstr(CfgSection("key"))
response.write(Msg )
end sub
</script>
++++++++++++++++++++++++
提示错误: System.NullReferenceException: 未将对象引用设置到对象的实例。
指向:Dim Msg As String = cstr(CfgSection("key"))
求教了!!

解决方案 »

  1.   

    System.Web.HttpContext.Current.GetConfig("test")得到的并不是一个hashtable对象,所以不能实现这个类型转换那么CfgSection就是个为定义的对象,不能访问
      

  2.   

    正确的写法是:Dim CfgSection As System.Collections.Specialized.NameValueCollection = System.Configuration.ConfigurationSettings.GetConfig("test") Dim Msg As String = cstr(CfgSection("key"))
      

  3.   

    config.web中的内容
    ++++++++++++++++++++++++++++++
    <configuration> 
      <system.web>
    <compilation debug="true"/>
               </system.web>
    <appSettings>
      <add key="ConnectionString1" value="Provider=sqloledb;Data source=10.2.10.10;initial catalog=northwind;password=as;user id=sa;"/>
      <add key="ConnectionString2" value="Provider=sqloledb;Data source=10.2.11.10;initial catalog=master;password=as;user id=sa;"/>
    </appSettings> 
    </configuration>
    ++++++++++++++++++++++++++++++++++++dim a as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString1")
    dim b as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString2")
      

  4.   

    谢谢,照你的代码,我编了个页面teamp.aspx
    ++++++++++++++++++++++
    <%@ Page Language="vb" Debug="true" %>haha<script runat="server">
    sub page_load(sender as object ,e as eventargs) 
    dim a as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString1")
    dim b as string = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString2")
    response.write(b)
    end sub
    </script>
    +++++++++++++++++
    可是为什么写不出来呢,是空白啊。
      

  5.   

    怎么把config.web命名成web.config就可以?奇怪,这个命名有关系吗?