开发者

How to open a SectionGroup in an ASP.NET web application?

开发者 https://www.devze.com 2023-04-01 05:53 出处:网络
I have a small ASP.NET web application hosted in an integration test (executing within NUnit).My product code can usually find configuration data from the web.config or app.config file, but for some r

I have a small ASP.NET web application hosted in an integration test (executing within NUnit). My product code can usually find configuration data from the web.config or app.config file, but for some rea开发者_运维技巧son when hosting ASP.NET I seem to get an ArgumentException when executing the first of these commands:

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

exePath must be specified when not running inside a stand alone exe.

I don't know what to put here. There isn't a sensible exePath for my product to ever pass into this method as a parameter as it usually runs within a web server. Also, ordinary Sections (not SectionGroups) can typically be opened using:

ConfigurationManager.GetSection(SectionName)

even within unit tests this works, where an App.config file somehow magically gets read. That's what I'd like when reading SectionGroups.

Any ideas?


Inside the web application, try using WebConfigurationManager. You will need a mechanism to detect if you are in a web context or exe context and use some design pattern to switch between contexts. A simple way to do this is check if HttpContext.Current is null (not null indicates web context and a null value indicates a exe context).

IMO, something like this should work,

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

It will be more complex if you don't want the dependency on the System.web dll

I haven't tested it.


ConfigurationManager.GetSection("SectionGroupName/GroupName")

i.e. e.g.

<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.Configuration.Configuration config =
    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
return config.GetSectionGroup(SectionGroupName);

should work in asp.net.


Posting in case this helps anyone. I used this answer to be able to read an Umbraco configuration.

    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;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消