Skip to main content
 首页 » 编程设计

c#之为不同的 Web 应用程序共享相同的配置设置

2024年02月13日32shanyou

这是我今天面试中被问到的问题。

How do i share same configuration settings for different web applications running under a same IIS.

我回答了,

Can be done by moving same into root level web.config/machine.config file under a CLR version i.e Windows/Microsoft VS .Net/Framework/Version/Config/filename

Have a xml file or some file located on a disk of the server and read using System.IO.

Older way, put some .ini file and read it

最后他们说全错了!想知道执行此操作的方法是什么?

请您参考如下方法:

一个选项:您可以在任何配置元素上使用 configSource 属性,让它从外部文件中提取配置。

ConnectionStrings.config

<connectionString> 
    ... 
</connectionStrings> 

Web.config

<connectionStrings configSource="ConnectionStrings.config" /> 

然后,您可以在您的项目之间链接 ConnectionStrings.config,一切顺利。