0
ASP.NET MVC4 응용 프로그램은 web.config 파일에서 목록을 읽어야합니다.web.config에서 목록을 정의하고 열거하는 방법
IList<ConnectionSection> connection =
(IList<ConnectionSection>)ConfigurationManager.GetSection("connectionGroup");
foreach (var s in connection)
Debug.WriteLine(s.Url);
은 그렇게 그 Web.config의가 포함 할 수있는이 문제를 해결하는 방법 오류를
Parser Error Message: Sections must only appear once per config file. See the help topic <location> for exceptions.
Source Error:
Line 19: <connectionGroup>
Line 20: <connection url="site1" database="site1" />
Line 21: <connection url="site2" database="site2" />
Line 22: </connectionGroup>
를 생산 실행 :
Web.config의 그것을 열거 할
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="connectionGroup">
<section
name="connection"
type="Helpers.connectionSection"
allowLocation="true"
allowDefinition="Everywhere"
/>
</sectionGroup>
</configSections>
<connectionGroup>
<connection url="site1" database="db1" />
<connection url="site2" database="db2" />
</connectionGroup>
코드입니다 포함 여러 연결 요소?
섹션으로 정의된다 :
namespace Helpers
{
public class ConnectionSection : ConfigurationSection
{
[ConfigurationProperty("url", IsRequired = true, IsKey = true)]
public string Url
{
get { return (string)this["url"]; }
set { this["url"] = value; }
}
[ConfigurationProperty("database", DefaultValue = "mydb", IsRequired = false)]
public string Database
{
get { return (string)this["database"]; }
set { this["database"] = value; }
}
}
}
및 호출 (ConnectionGroupConfigurationSection 같은 WebConfigurationManager.GetSection (ConnectionGroupConfigurationSection.NodeName)) .ConnectionGroup.Cast() –
Nathan