【问题标题】:web.config custom ConfigurationSection and unnecessary verbosityweb.config 自定义 ConfigurationSection 和不必要的冗长
【发布时间】:2009-09-01 07:42:23
【问题描述】:

除非我做错了什么,否则我应该使用 ConfigurationSection、ConfigurationElement 和 ConfigurationElementCollection 的方式需要我像这样格式化我的配置部分:

<serviceAuthorization>
    <credentials>
        <login username="system" password="password" mode="include">
            <services>
                <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" />
                <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" />
            </services>
        </login>
        <login username="test" password="pass" mode="exclude" />
    </credentials>
</serviceAuthorization>

如果我对格式有更多的发言权,我会更喜欢。我想这样格式化我的部分:

<serviceAuthorization>
    <login username="system" password="password" mode="include">
        <service type="AxeFrog.Mobile.Service.Security.AuthenticationService, AxeFrog.Mobile.Service" />
        <service type="AxeFrog.Mobile.Service.Security.AnotherService, AxeFrog.Mobile.Service" />
    </login>
    <login username="test" password="pass" mode="exclude" />
</serviceAuthorization>

有没有办法获取配置部分的 XML 并自己阅读?

【问题讨论】:

    标签: c# .net web-config configuration-files


    【解决方案1】:

    你可以实现System.Configuration.IConfigurationSectionHandler并进行配置:

    <section name="serviceAuthorization" type="[your-type]"/>
    

    然后您将整个section 设为XmlNode,并可以解析您的自定义架构。

    edit:已弃用。这里有一个new way 来做。

    【讨论】:

    • 据我了解,IConfigurationSectionHandler 已被弃用
    【解决方案2】:

    嗯,你可以这样做,例如:

    string docName=System.Web.HttpContext.Current.Server.MapPath("Web.config");
    XmlDocument configDoc = new XmlDocument();
    configDoc.Load(docName);
    

    然后从configDoc工作。

    【讨论】:

    • 我知道那条路线;我只是希望有一些不那么粗糙的东西。
    猜你喜欢
    • 2013-01-22
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2011-10-03
    相关资源
    最近更新 更多