引用项目中的 ASP.NET 自定义配置部分不起作用?

发布于 2024-11-09 05:57:18 字数 1634 浏览 0 评论 0原文

我创建了一个继承自 System.Configuration.ConfigurationSection 的自定义类,其实现如下(这显然只是为了让真正的配置运行):

public class Config : ConfigurationSection
{
    [ConfigurationProperty("quoteOfTheDay", DefaultValue = "It is what it is.", IsRequired = false)]
    public string QuoteOfTheDay
    {
        get
        {
            return this["quoteOfTheDay"] as string;
        }
    }

    [ConfigurationProperty("yourAge", IsRequired = true)]
    public int YourAge
    {
        get
        {
            return (int)this["yourAge"];
        }
    }
}

完整的命名空间 + 类的名称是 MyApp。 Core.Config,它位于 MyApp.Core 项目中,该项目编译为 MyApp.Core.dll 程序集。然后,我从我的 ASP.NET/MVC3 项目(称为 MyApp.UI)引用了该项目。我已经编辑了 Web.config 文件以添加 sectionGroupsection 元素,如下所示:

  <configSections>
    <sectionGroup name="myAppConfigGroup">
      <section name="myAppSection" 
           type="MyApp.Core.Config, MyApp.Core, Version=1.0.0.0, Culture=neutral, PublicKey=null" 
           allowLocation="true" 
           allowDefinition="Everywhere" />
    </sectionGroup>
  </configSections>

  <myAppConfigGroup>
    <myAppSection>

    </myAppSection>
  </myAppConfigGroup>

但是,它看起来不像类型MyApp.Core.Config 用于验证配置或在编辑配置时提供智能感知。以下是我的一些观察结果:

  • 我将 yourAge 属性定义为 IsRequired = true,并且应用程序在没有它的情况下也可以正常运行。
  • 我什至可以更改
    元素中的类型(更改为 Foo.Bar 等根本不存在的类型),并且一切仍然运行正常
  • 但是如果我删除 < ;section> 元素,应用程序无法运行。

我很困惑,有什么帮助吗?

I've created a custom class that inherits from System.Configuration.ConfigurationSection, implemented like this (this is obviously just to get the real configuration running):

public class Config : ConfigurationSection
{
    [ConfigurationProperty("quoteOfTheDay", DefaultValue = "It is what it is.", IsRequired = false)]
    public string QuoteOfTheDay
    {
        get
        {
            return this["quoteOfTheDay"] as string;
        }
    }

    [ConfigurationProperty("yourAge", IsRequired = true)]
    public int YourAge
    {
        get
        {
            return (int)this["yourAge"];
        }
    }
}

The full namespace + name of the class is MyApp.Core.Config, it's located in the MyApp.Core project which compiles to the MyApp.Core.dll assembly. I've then referenced this project from my ASP.NET/MVC3 project, which is called MyApp.UI. I've edited the Web.config file to add the sectionGroup and section elements like this:

  <configSections>
    <sectionGroup name="myAppConfigGroup">
      <section name="myAppSection" 
           type="MyApp.Core.Config, MyApp.Core, Version=1.0.0.0, Culture=neutral, PublicKey=null" 
           allowLocation="true" 
           allowDefinition="Everywhere" />
    </sectionGroup>
  </configSections>

  <myAppConfigGroup>
    <myAppSection>

    </myAppSection>
  </myAppConfigGroup>

However, it doesn't seem like the type MyApp.Core.Config get used to validate the configuration or provide intelli-sense as I edit the configuration. Here's some observations I've made:

  • I defined the yourAge property as IsRequired = true and the application runs fine without it.
  • I can even change the type (to some type that doesn't even exist like Foo.Bar) in the <section> element, and everything still runs fine
  • But if I remove the <section> element, the app doesn't run.

I'm baffled, any help?

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

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

发布评论

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

评论(1

蒲公英的约定 2024-11-16 05:57:18

您可能没有使用该配置?
如果您不使用它,它只会检查根节点是否通过 configSections 注册,但不会查看此时的详细信息。

当您实际使用 ConfigurationManager.GetSection 加载它时,将验证设置,并且在您的配置中,它将触发一个异常,告知缺少所需的 YourAge 属性。

You are probably not using the configuration?
If you are not using it, it will only do a check if the root nodes are registered via configSections but it will not look at the details at that point.

When you will actually load it using ConfigurationManager.GetSection the settings will be validated and in your case of this configuration it will trigger an exception telling that the required YourAge property is missing.

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