Search code examples
c#web-configwebconfigurationmanager

Find out which web.config(s) have been loaded


I'm encountering an error indicating that the web.config being loaded by this particular sub-project of my solution has a connectionstring that conflicts with an existing entry from an already-loaded web.config (Exception message is: Additional information: The entry 'connStr' has already been added.)

Is there a way to easily find out the list of all web.configs loaded/being loaded, so that I can ascertain where to conflict is arising


Solution

  • There is only one web.config that will be loaded, however, it will inherit from your machine configuration. For IIS this is here:

    %windir%\Microsoft.NET\Framework\framework_version\CONFIG\machine.config
    

    And for IIS Express in one of these places:

    %userprofile%\documents\iisexpress\config\applicationhost.config
    %userprofile%\my documents\iisexpress\config\applicationhost.config
    $(solutionDir)\.vs\config\applicationhost.config (only for Visual Studio 2015 and above)
    

    So you can remove the duplicate from there, or add a remove entry in your web.config, for example:

    <connectionStrings>
      <remove name="MyConnection" />
      <add name="MyConnection" connectionString="..." providerName="System.Data.SqlClient" />
    </connectionStrings>