Web.Config 中的自定义部分/集合

发布于 2024-12-09 19:35:30 字数 573 浏览 0 评论 0 原文

我有一堆路由,我希望能够将它们放入我的 Web.Config 文件中。我需要为集合中的每个部分/项目提供一个键和两个值字段。类似这样的事情......

<routes>
    <add
        key="AdministrationDefault"
        url="Administration/"
        file="~Administration/Default.aspx" />

    <add
        key="AdministrationCreateCampaign"
        url="Administration/CreateCampaign/"
        file="~/Administration/CreateCampaign.aspx" />

    <add
        key="AdministrationLogout"
        url="Administration/Leave/"
        file="~/Administration/Leave.aspx" />
</routes>

这可能吗?

I've got a bunch of routes that I want to be able to throw in my Web.Config file. I need one key and two value fields for each section/item in the collection. Something along the lines of this...

<routes>
    <add
        key="AdministrationDefault"
        url="Administration/"
        file="~Administration/Default.aspx" />

    <add
        key="AdministrationCreateCampaign"
        url="Administration/CreateCampaign/"
        file="~/Administration/CreateCampaign.aspx" />

    <add
        key="AdministrationLogout"
        url="Administration/Leave/"
        file="~/Administration/Leave.aspx" />
</routes>

Is this possible?

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

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

发布评论

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

评论(2

初相遇 2024-12-16 19:35:30

是的。一旦开始,就不会太难。

您需要创建一个 ConfigurationSection 派生类来定义 部分(然后将

添加到配置将 元素链接到您的类型)。

然后,您需要一个类型来定义集合的每个元素,并需要标记为默认值的集合的第二个类型的属性。

设置完所有这些后,在运行时您可以访问配置部分:

var myRoutes = ConfigurationManager.GetSection("routes") as RoutesConfigSection;

我的博客有几篇关于此背景的文章:http://blog.rjcox.co.uk/category/dev/net-core/

正如另一个答案也有覆盖范围(比以前好很多)在 MSDN 上。

Yes. And not too hard once you have a start.

You'll need to create a ConfigurationSection derived class to define the <routes> section (and then add a <section> to the configuration to link the <routes> element to your type).

You'll then need a type to define each element of the collection and, flagged as default, a property on your second type for the collection.

After all this is set up, at runtime you access your configuration section as:

var myRoutes = ConfigurationManager.GetSection("routes") as RoutesConfigSection;

My blog has a few articles on the background to this: http://blog.rjcox.co.uk/category/dev/net-core/

As noted in another answer there is also coverage (a lot better than it used to be) on MSDN.

纵山崖 2024-12-16 19:35:30

如果您不想创建一个类来表示您的配置部分,您可以执行以下操作:

var configSection = ConfigurationManager.GetSection("sectionGroup/sectionName");
var aValue = (configSection as dynamic)["ValueKey"];

转换为动态可让您访问 configSection 中的键值。您可能需要在 configSection 中添加断点和峰值,以查看其中有什么以及要使用什么 ValueKey。

If you don't want to create a class to represent your config section you can do this:

var configSection = ConfigurationManager.GetSection("sectionGroup/sectionName");
var aValue = (configSection as dynamic)["ValueKey"];

Converting to dynamic lets you access the key values in configSection. You may have to add a break point and peak in configSection to see what is there and what ValueKey to use.

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