Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a web.config read from an external xml file?

Tags:

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.

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.

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

like image 226
DotnetDude Avatar asked Mar 09 '10 17:03

DotnetDude


People also ask

Is Web config an XML file?

The Web. config file must be a well-formed XML document and must have a format similar to the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\Machine.

What is Web config file Why do we use Web config file?

The web. config is a file that is read by IIS and the ASP.NET Core Module to configure an app hosted with IIS.

What does Web config file contain?

A web. config file is a Windows file that lets you customize the way your site or a specific directory on your site behaves. For example, if you place a web. config file in your root directory, it will affect your entire site (www.coolexample.com).


2 Answers

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:

<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>
    <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.

like image 71
marc_s Avatar answered Oct 25 '22 22:10

marc_s


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.

like image 43
Tom Brothers Avatar answered Oct 26 '22 00:10

Tom Brothers