怎么样正确得到在web.config中设的值?

raozr 2003-05-27 02:07:53
我的web.config.

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
<configSections>
<sectionGroup name="test">
<section name="SimpleSettings"
type="System.Configuration.NameValueSectionHandler" />

</sectionGroup>
</configSections>

<test>
<SimpleSettings>
<add key="myurl" value="/Test/finalpage.aspx" />
</SimpleSettings>
</test>


而在ASP.net 的文件中写:

System.Collections.Specialized.NameValueCollection SimpleSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig("Test/SimpleSettings");

而这个SimpleSettings 总是为null.

是哪出问题了, 各位帮帮忙!!
...全文
111 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
New_bug 2003-06-03
  • 打赏
  • 举报
回复
<configuration>
<appSettings>
<add key="sConn1" value="Server=(local);User ID=sa;Password=sa;Database=xxx"/>
<add key="s2" value="ccc@ss.com"/>
</appSettings>

.vb:

Application("sConn") = ConfigurationSettings.AppSettings("sConn1")
Application("sEmail") = ConfigurationSettings.AppSettings("s2")
小李飞刀 2003-06-03
  • 打赏
  • 举报
回复
to raozr:
问题出在自定义配置节需要自己写配置节处理程序,如果不想自己来处理配置节,把配置信息放在appSettings中好了。
raozr 2003-05-29
  • 打赏
  • 举报
回复
to avyp() :

我在MSDN上查到了 Declaring and Accessing Section Groups
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondeclaringsectiongroups.asp
代码里是少了一个assembly name . 根据MSDN, 我把代码改为,
<section name="SimpleSettings" type="System.Configuration.NameValueSectionHandler,System" />

还是出现错误信息为:

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: File or assembly name System, or one of its dependencies, was not found.

还是
<section name="SimpleSettings" type="System.Configuration.NameValueSectionHandler,System" />

这行有问题. 你能帮忙看一下吗?
小李飞刀 2003-05-28
  • 打赏
  • 举报
回复
to mummy_zc:比较自定义配置节和appSettings,我想主要有以下几个方面的差异:
1. 使用自定义配置节不必在每一次使用配置信息时都去读Config文件。
2. 自定义配置节允许对配置节作更多的控制,可以构照更复杂的应用。
3. 自定义配置节比appSettings更加面向对象,应该是专业的软件开发人员的首选。
okok999 2003-05-27
  • 打赏
  • 举报
回复
本地的web.config 到服务起就不行了为什么啊!高手门
升烟 2003-05-27
  • 打赏
  • 举报
回复
to avyp()能否请教一下在什么情况下比较需要自定义配置节,使用appSettings不行吗?谢谢
小李飞刀 2003-05-27
  • 打赏
  • 举报
回复
问题应该出在: <section name="SimpleSettings"
type="System.Configuration.NameValueSectionHandler" />

type的格式应该为"class,assembly",其中class应该提供提供配置节处理程序,即实现接口IConfigurationSectionHandler。正确定义后你的SimpleSettings 就不会是null了。
.Net处理自定义配置节的机制非常好,我一直在用。
升烟 2003-05-27
  • 打赏
  • 举报
回复
web.config中的节都是在machine.config中定义过的,你要想用自己自定义的节必须也要在machine.config中定义,自己写一个继承IConfigurationSectionHandler的类,有点烦.建议你还是用<appSettings>
coolnick 2003-05-27
  • 打赏
  • 举报
回复
在web.config中:
<appSettings >
<add key="strConn" value="server=127.0.0.1;uid=sa;pwd=;database=car"/>
</appSettings>

然后取值时:
configurationsettings.appSettings("strConn")
raozr 2003-05-27
  • 打赏
  • 举报
回复
up
raozr 2003-05-27
  • 打赏
  • 举报
回复
我是要对到对像, 可以下面这么用.

for(int x = 0;x < SimpleSettings.Count;x++)
{
string source=SimpleSettings.GetKey(x);
string dest = SimpleSettings.Get(x);

NewUrl(dest, Appl);
break;
}
}
dongchuanlu 2003-05-27
  • 打赏
  • 举报
回复


<test>
<SimpleSettings>
<add key="myurl" value="/Test/finalpage.aspx" />
</SimpleSettings>
</test>


</configSections>
dongchuanlu 2003-05-27
  • 打赏
  • 举报
回复
System.test.SimpleSettings.AppSettings["myurl"];

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧