1

I have inherited some very old asp code. I need to resolve a design problem because of it, described below.

I have 3 customers... Foo, Bar and Baz.

I have a folder called...

c:\WebSites\Site.v15.07

I have 3 websites in IIS defined as...

Website.Foo

Website.Bar

Website.Baz

They all point to the 15.07 directory.

There are 3 other folders called...

c:\WebConfigurations\Foo

c:\WebConfigurations\Bar

c:\WebConfigurations\Baz

... which all contain client specific files for each of the 3 sites. Uploaded images etc.

Each of the 3 customers has their own database that the website sits on.

I need to set (ideally, using a web.config) the connection string for each of the 3 sites.

If I put this in the web.config in the root of the website directory, they will all share the same setting.

Is there a way of adding the settings/web.config in IIS at the "website" level so that each site can be set differently?

Community
  • 1
  • 1
Beakie
  • 1,948
  • 3
  • 20
  • 46
  • 1
    You can try using this solution: https://stackoverflow.com/questions/45889661/several-asp-net-sites-that-are-sharing-application-files-but-using-different-th – Vadim Panov Aug 25 '17 at 21:36

1 Answers1

1

Put only the common configuration in Web.config in the application folder.

When the application is first loading, programmatically load the client-specific config file from the respective folder and programmatically merge the info into the configuration info in memory.

Example of programmatically editing configuration info in memory:

Change a web.config programmatically with C# (.NET)

var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";

In your case you'll want to make two calls to WebConfigurationManager.OpenWebConfiguration, one to open the application config and once to load the client configuration. Then copy data from client config to the in-memory application config (don't call Save() per the referenced question--that's a different use case).

Community
  • 1
  • 1
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • I have tried this but it seems that without calling save(), using ConfigurationManager doesn't use the settings in memory. Is this correct? – Beakie Oct 07 '15 at 10:18
  • @Beakie, `Save()` will persist the changes to disk. You don't need to call it if you only want to affect the settings in memory. In my own usage I adjust setting sin memory from an outside source when needed but have never persisted the changes to disk. – Samuel Neff Oct 07 '15 at 17:22
  • But the moment I dispose of the WebConfigurationManager variable, the settings are forgotten. Also ConfigurationManager doesn't reflect the changes made via WebConfigurationManager. Am I doing this wrong? – Beakie Oct 08 '15 at 08:56
  • @Beakie, I'm not following what you're saying. `WebConfigurationManager` is used statically, so it's never disposed. I would expect that opening the configuration from both `WebConfigurationManager` and `ConfigurationManager` independently will load two separate copies of the config into memory. Changes to one will not be reflected in the other. – Samuel Neff Oct 08 '15 at 17:32