【问题标题】:How to open a SectionGroup in an ASP.NET web application?如何在 ASP.NET Web 应用程序中打开 SectionGroup?
【发布时间】:2011-11-08 21:19:10
【问题描述】:

我有一个托管在集成测试中的小型 ASP.NET Web 应用程序(在 NUnit 中执行)。我的产品代码通常可以从 web.config 或 app.config 文件中找到配置数据,但由于某种原因,在托管 ASP.NET 时,我在执行这些命令中的第一个时似乎得到了 ArgumentException

var configuration = ConfigurationManager.OpenExeConfiguration(null);
return configuration.GetSectionGroup(SectionGroupName);

不在独立 exe 中运行时必须指定 exePath。

我不知道该放什么。我的产品没有一个明智的 exePath 可以作为参数传递给此方法,因为它通常在 Web 服务器中运行。此外,通常可以使用以下方法打开普通部分(不是 SectionGroups):

ConfigurationManager.GetSection(SectionName)

即使在单元测试中这也有效,其中 App.config 文件以某种方式神奇地被读取。这就是我在阅读 SectionGroups 时想要的。

有什么想法吗?

【问题讨论】:

    标签: asp.net configurationmanager


    【解决方案1】:

    在 Web 应用程序中,尝试使用 WebConfigurationManager。您将需要一种机制来检测您是在 web 上下文还是 exe 上下文中,并使用某种设计模式在上下文之间切换。 一个简单的方法是检查 HttpContext.Current 是否为空(非空表示 web 上下文,空值表示 exe 上下文)。

    IMO,这样的东西应该可以工作,

            Configuration configuration;
            if (HttpContext.Current == null)
                configuration = ConfigurationManager.OpenExeConfiguration(null); // whatever you are doing currently
            else
                configuration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath); //this should do the trick
    
            configuration.GetSectionGroup(sectionGroupName);
    

    如果你不想依赖 System.web dll 会更复杂

    我没有测试过。

    【讨论】:

    • 谢谢。至少在托管的 ASP.NET 案例中,HttpContext.Current.Request.ApplicationPathnull,我从机器的 web.config 而不是网站的 web.config 文件中获取 Configuration 对象。有什么想法吗?
    • 嗯,需要一种机制来获取虚拟路径。你可以试试appdomainappvirtualpath。但如果 onof 的解决方案有效,这听起来是一种更简单的方法。
    • @onof 的解决方案对我不起作用,HttpContext.Current.Request 在我调用它时不可用,但 WebConfigurationManager.OpenWebConfiguration("/"); 工作正常
    • 使用OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath); 没有必要,恕我直言。
    【解决方案2】:
    System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    return config.GetSectionGroup(SectionGroupName);
    

    应该在 asp.net 中工作。

    【讨论】:

    • @onof 对我不起作用(.NET 4.5)...:/我错过了什么吗?
    • 您可以用于ASP.NETif (System.Web.HttpContext.Current != null) configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    【解决方案3】:
    ConfigurationManager.GetSection("SectionGroupName/GroupName")
    

    即例如

    <configSections>
        <sectionGroup name="RFERL.Mvc" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
            <section name="routes" type="RFERL.Mvc.Configuration.RoutesConfigurationSection, RFERL.Mvc"/>
        </sectionGroup> 
    </configSections>
    

    &

    var config = ConfigurationManager.GetSection("RFERL.Mvc/routes") as RoutesConfigurationSection;
    

    【讨论】:

    • 这对我有用,但使用 System.Web.Configuration.WebConfigurationManager.GetSection
    【解决方案4】:

    张贴以防万一这对任何人都有帮助。我使用这个答案能够读取 Umbraco 配置。

        private static string CdnUrl()
        {
            // Unit test vs web environment
            Configuration configuration;
            if (HttpContext.Current == null)
            {
                configuration = ConfigurationManager.OpenExeConfiguration(null);
            }
            else
            {
                configuration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            }
    
            // Grab Umbraco config
            ConfigurationSectionGroup umbracoConfiguration = configuration.GetSectionGroup("umbracoConfiguration");
            FileSystemProvidersSection fileSystemProviders = (FileSystemProvidersSection)umbracoConfiguration.Sections.Get("FileSystemProviders");
    
            // Return the information needed
            var cdnUrl = fileSystemProviders.Providers["media"].Parameters["rootUrl"].Value;
            return cdnUrl;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 2019-10-09
      • 1970-01-01
      • 2019-08-12
      相关资源
      最近更新 更多