Single Website Directory / Multiple IIS Websites (Multiple web.config files)

南笙酒味 提交于 2020-08-09 19:33:08

问题


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?


回答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).



来源:https://stackoverflow.com/questions/31519750/single-website-directory-multiple-iis-websites-multiple-web-config-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!