web.config 自定义 ConfigurationSection 和不必要的冗长

发布于 2024-08-03 17:18:41 字数 1248 浏览 7 评论 0原文

除非我做错了什么,否则我应该使用 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 并自己阅读它?

Unless I am doing something wrong, the way I am supposed to use ConfigurationSection, ConfigurationElement and ConfigurationElementCollection, would require me to format my configuration section like so:

<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>

I would much prefer if I had a bit more say in the format. I would like to format my section like this:

<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>

Is there a way I can get the XML of the configuration section and just read it myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-08-10 17:18:41

您可以实现 System.Configuration.IConfigurationSectionHandler 并配置它:

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

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

编辑:这已被弃用。这是一种新方法它。

You can implement System.Configuration.IConfigurationSectionHandler and configure it:

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

Then you get your entire section as XmlNode and can parse your custom schema.

edit: this is deprecated. here is one new way to do it.

梦在深巷 2024-08-10 17:18:41

好吧,您可以这样做,例如:

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

然后从 configDoc 开始工作。

Well, you could do, for example:

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

and then work from configDoc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文