Two sites running same code and different config files

iis-7.5windows-server-2008-r2

I have a Windows 2008R2 server with IIS, running one site (ASP.NET4.5). All the parameters are written in web.config file.

I have to add a new site, that will run on the same code (same root folder) as the first one, but will read parameters like sql connection strings etc. from its own config file, not from first site web.config.

How can I do that?

Is it possible to run the second site in different app pool?
Both sites will run the same .NET version of course.

Thank you!

Best Answer

Yes. I've served the exact same code/files from multiple AppPools for years; it works fine.

But all instances will read web.config identically since Microsoft's standard functionality wasn't written to do what you're trying to do. So you will have to implement your own custom solution for determining what instance the code is running under and choose the appropriate configuration variables based on that. You could either store your config data in a custom config section within web.config (defined in "configSections") or have a separate .config file somewhere else.

What I usually do is switch the code's behavior based on the path to the IIS Application it's running under (i.e. the path). E.g. if the site is being served up under "/example1", I pick one set of config data, and if it's served up under "/example2" it gets another set. (Note that you'll also want to handle the config for the Dev Server's URL when you're running your site through VisualStudio.)