views:

541

answers:

4

I have an ASP.NET application (Root Application) that has a virtual directory set up to another ASP.NET application (Virtual Application). How can I make the Virtual Application read values from the Root Application's web.config file? I was looking at the WebConfigurationManager.OpenWebConfiguration() class, but I'm unsure how how to tell it to go up one level from the root. For example, I would tell it to go to ~/web.config to get the the Virtual Application's web.config file, but I need it to go up one more level to the Root Application's file structure. Is this even the correct approach?

Any help would be greatly appreciated!!

+2  A: 

You can use the ExeConfigurationFileMap class with ConfigurationManager, like:

string configFile = new FileInfo(Server.MapPath("/Web.config")).Directory.Parent.FullName + "\\Web.config";

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();

fileMap.ExeConfigFilename = configFile;

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

Response.Write(config.AppSettings.Settings["Test"].Value);
Langdon
+1  A: 

IIS does have programmatic access to its configuration data (which is documented on MSDN and/or Technet). This will be the only supported route (i.e. will continue to work across IIS versions).

Otherwise you can hack a solution (both of these will require higher than usual rights for the process):

  • Parse the output from appcmd.exe:

    E.g. here:

    > C:\Windows\system32\inetsrv\appcmd.exe list vdir
    VDIR "Default Web Site/" (physicalPath:E:\Dev\weblocal\XYZ)
    VDIR "Default Web Site/DevRoot/TestWebClient" (physicalPath:E:\Dev\Tests\ClientSideWeb)
    VDIR "Default Web Site/Home" (physicalPath:E:\Data\Homepages)
    
  • Read the configuration directly from C:\Windows\System32\inetsrv\config.

Richard
A: 

I think you will find that your desired behavior is in fact the default behavior.

web.config settings cascade down. Your app will look up to the next hierarchical web.config if it can't find the value.

This would allow you to just look up via AppSettings etc. for most cases.

I'm not sure what happens if you really need to direct access to the file as opposed to the various config api access methods.

I have a setup like this right now using IIS7 with multiple virtual apps configured.

HectorMac
Interesting. I have the same config value set in my Virtual Directory Application and my Root Directory Application. When I remove the value from the Virtual Directory Application I would have expected it to read from the Root Directory Application, but I got an error instead.
Mike C.
I have set of apps that find an AppSettings value from the hosting app without any special configuration. All the virtual apps are configured as such in IIS7. Value only exists in hosting web.config.
HectorMac
Interesting. I wonder what I'm doing wrong. Is your Virtual Application set up as an application in IIS?
Mike C.
A: 

I tried HectorMac's suggestion again and it still doesn't work for me. As a result I am going to seek an alternative to storing my value in the web.config file.

Mike C.