web.config可以从外部xml文件中读取吗?

时间:2023-01-16 15:48:19

I have to duplicate some settings (like connection string) between a web.config file that a WCF host uses and a web.config file that a web client uses.

我必须复制WCF主机使用的web.config文件和Web客户端使用的web.config文件之间的某些设置(如连接字符串)。

In the interest of not duplicating, can I have both the web.configs read from a separate xml file? The two web.configs can be entirely in different solutions/projects so I guess this is not possible, but wanted to get other's opinion.

为了不重复,我可以从单独的xml文件中读取web.configs吗?两个web.configs可以完全在不同的解决方案/项目中,所以我想这是不可能的,但想得到别人的意见。

PS: I do understand I can use a database to store all the config settings.

PS:我明白我可以使用数据库来存储所有配置设置。

4 个解决方案

#1


19  

Yes, any configuration section can be "externalized" - this includes things like <appSettings>, <connectionStrings> and many more.

是的,任何配置部分都可以“外部化” - 这包括 等等。

You'd have something like this in your web.config:

你的web.config中有这样的东西:

<configuration>
   <appSettings configSource="appSettings.config" />   
   <connectionStrings configSource="connectionStrings.config" />
   <system.web>    
      <pages configSource="pages.config" />
      <httpHandlers configSource="httphandlers.config">
   </system.web>    
</configuration>

The externalized config's would just contain that one subsection in them:

外化配置只包含其中的一个子部分:

httphandlers.config:

httphandlers.config:

<httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>

Note you cannot externalize the entire <system.web> part, since that is a configuration section group - not a configuration section - but you can externalize most of the sub-sections contained in system.web.

请注意,您无法外部化整个 部分,因为这是一个配置节组 - 而不是配置节 - 但您可以外部化system.web中包含的大多数子节。

#2


11  

A config file can point to other config files as long as the files are in the same path (including subdirectories).

只要文件位于同一路径(包括子目录),配置文件就可以指向其他配置文件。

Here is an example of my config settings:

以下是我的配置设置示例:

<connectionStrings configSource="web\config\connectionStrings.config" />
<appSettings configSource="web\config\appSettings.config" />
<system.diagnostics configSource="web\config\diagnostics.config" />
<system.serviceModel>
    <bindings configSource="web\config\serviceModelBindings.config" />
    <behaviors configSource="web\config\serviceModelBehaviors.config" />
    <services configSource="web\config\serviceModelServices.config" />
    <client configSource="web\config\serviceModelClient.config" />
</system.serviceModel>

In my case, I have several windows applications in a root folder which include a web application as a subfolder. This allows each application's config file to point to the shared configs.

在我的例子中,我在根文件夹中有几个Windows应用程序,其中包含一个Web应用程序作为子文件夹。这允许每个应用程序的配置文件指向共享配置。

#3


1  

if you can, just put the configurations in common in the Machine.Config. They both can read from there, and each web.config can override any values you need.

如果可以,只需将配置放在Machine.Config中。它们都可以从那里读取,每个web.config都可以覆盖您需要的任何值。

I say if you can, because I know that many hosting companies won't let you modify it unless you are renting a dedicated server.

我说如果可以的话,因为我知道许多托管公司不会让你修改它,除非你租用专用服务器。

#4


1  

you can specify your connectionstrings outside of your web.config http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx but it has to be in the same directory.

你可以在你的web.config http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx之外指定你的连接字符串,但它必须在同一目录。

#1


19  

Yes, any configuration section can be "externalized" - this includes things like <appSettings>, <connectionStrings> and many more.

是的,任何配置部分都可以“外部化” - 这包括 等等。

You'd have something like this in your web.config:

你的web.config中有这样的东西:

<configuration>
   <appSettings configSource="appSettings.config" />   
   <connectionStrings configSource="connectionStrings.config" />
   <system.web>    
      <pages configSource="pages.config" />
      <httpHandlers configSource="httphandlers.config">
   </system.web>    
</configuration>

The externalized config's would just contain that one subsection in them:

外化配置只包含其中的一个子部分:

httphandlers.config:

httphandlers.config:

<httpHandlers>
    <remove verb="*" path="*.asmx"/>
    <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>

Note you cannot externalize the entire <system.web> part, since that is a configuration section group - not a configuration section - but you can externalize most of the sub-sections contained in system.web.

请注意,您无法外部化整个 部分,因为这是一个配置节组 - 而不是配置节 - 但您可以外部化system.web中包含的大多数子节。

#2


11  

A config file can point to other config files as long as the files are in the same path (including subdirectories).

只要文件位于同一路径(包括子目录),配置文件就可以指向其他配置文件。

Here is an example of my config settings:

以下是我的配置设置示例:

<connectionStrings configSource="web\config\connectionStrings.config" />
<appSettings configSource="web\config\appSettings.config" />
<system.diagnostics configSource="web\config\diagnostics.config" />
<system.serviceModel>
    <bindings configSource="web\config\serviceModelBindings.config" />
    <behaviors configSource="web\config\serviceModelBehaviors.config" />
    <services configSource="web\config\serviceModelServices.config" />
    <client configSource="web\config\serviceModelClient.config" />
</system.serviceModel>

In my case, I have several windows applications in a root folder which include a web application as a subfolder. This allows each application's config file to point to the shared configs.

在我的例子中,我在根文件夹中有几个Windows应用程序,其中包含一个Web应用程序作为子文件夹。这允许每个应用程序的配置文件指向共享配置。

#3


1  

if you can, just put the configurations in common in the Machine.Config. They both can read from there, and each web.config can override any values you need.

如果可以,只需将配置放在Machine.Config中。它们都可以从那里读取,每个web.config都可以覆盖您需要的任何值。

I say if you can, because I know that many hosting companies won't let you modify it unless you are renting a dedicated server.

我说如果可以的话,因为我知道许多托管公司不会让你修改它,除非你租用专用服务器。

#4


1  

you can specify your connectionstrings outside of your web.config http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx but it has to be in the same directory.

你可以在你的web.config http://blog.andreloker.de/post/2008/06/Keep-your-config-clean-with-external-config-files.aspx之外指定你的连接字符串,但它必须在同一目录。