一,在web.config中的configuration节点下我们可以添加自己的配置,如下:
<TestSectionGroup> <TestSection> <add key="Url" value="xxxxx" /> <add key="Token" value="xxxxx" /> <add key="Key" value="xxxxx" /> </TestSection> </TestSectionGroup>
二,但是刚开始这样添加是使用不了的,我们需要在configuration的configSections下注册
<configSections> <sectionGroup name="TestSectionGroup"> <section name="TestSection" type="System.Configuration.NameValueSectionHandler,System, Version=4.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089" /> </sectionGroup> </configSections>
三,调用代码
private static NameValueCollection TestSection = (NameValueCollection)ConfigurationManager.GetSection("TestSectionGroup/TestSection"); private static string Url= TestSection ["Url"]; private static string Token= TestSection ["Token"]; private static string Key= TestSection ["Key"];