简体   繁体   中英

Web.Config files and locations

We have a whole bunch of websites with very similar web.config files.

Can you centralise the duplicate configs in 1 config file before the root directory of each website? Or is the only option machine.config?

We are looking to centralise an assembly reference in the GAC.

Structure:

  • Containing Directory
    • Website 1 Directory
    • Website 2 Directory
    • Website 3 Directory
    • Web.Config File for all above sites

I have not encountered a way to have inherited config files besides machine.config, app/web.config and user.config levels. But you can use configSource attribute on all config sections (ConfigurationSection based) to include a common file for example with service endpoints, client endpoints, bindings, connection strings and others. Even though VS intellisense marks it as unsupported it does work.

<configuration>

    <system.serviceModel>
        <services configSource="Services.config" />
        <client configSource="Client.config" />
        <bindings configSource="Bindings.config" />
        <behaviors configSource="Behaviors.config" />
    </system.serviceModel>

    <pages configSource="pages.config"/>

</configuration>

Config source files must be in application's folder or any folder below. No going up or absolute paths. But there is a trick to overcome this limitation in VS2010. You need to add an existing file as a link and change its property named "Copy to Output Directory". This way your absolute path file will get copied to your application folder from where you can reference it in configSource. In previous versions of VS it is also possible but in a less elegant way - copy file in post build event.

If you are looking mainly to centralize WCF settings there is another option: in-code configuration. Huge advantage of this is you get compilation-time check and refactoring support from VS. If this does not sound like much I can assure you that in a bigger WCF project, config file management is a nightmare especially when you need to change something. With this approach it is also very easy to centralize WCF settings by just creating a common assembly where all services, endpoints, bindings etc. are defined. Disadvantage is that you loose possibility to change WCF settings without recompilation. But if those settings do not change very often it is a tempting alternative.

You can use the web.config located in

%SystemRoot%\Microsoft.NET\Framework\<versionNumber>\CONFIG\Web.config

Or if in IIS you configure your Containing directory as a main web site and then put your website directories as applications, you can put the web.config in the main web site to have the structure you mention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM