Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override machine.config by web.config

I learn to work with the built-in profile provider of .Net, and have the following problem:

I read that the machine.config-settings can be overridden by the web.config-settings of a .Net-Application. The following settings in the machine.config-file are relevant for me:

<connectionStrings>
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;
Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

<profile><providers><add name="AspNetSqlProfileProvider"connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/></providers></profile>

These settings work to set up local profiles. However, when I copy the settings into the web.config of my application and change the machine.config settings, so that they don´t work any more, I get a configuration error. For example, I change the name of the provider in the machine.config to "Local". This should be no problem, because the settings are overridden. However, when running the application I get the error:

"The entry "AspNetSQLProvider has already been added" (my translation)

like image 982
AGuyCalledGerald Avatar asked Feb 04 '10 16:02

AGuyCalledGerald


People also ask

How do I bypass web config?

You can't override machine. config by default it gets the default setting from it and the section can be overridden in the app. config for windows application and web. config for web application.

What is difference between machine config and web config?

config ? The web. config files specify configuration settings for a particular web application, and are located in the application's root directory; the machine. config file specifies configuration settings for all of the websites on the web server, and is located in $WINDOWSDIR$\Microsoft.Net\Framework\Version\Config.

Can we dynamically change the key value in web config?

This is simple and useful application for those who wants dynamic changes in the web. config file. The sample source code allows you to change the key and value pair. There is no need to open web.

Where is the machine level web config?

The Machine. config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ directory. In the Machine. config file, locate the configuration setting you want to override in your Web.


1 Answers

Add a <clear /> element as the first child of <connectionStrings>. It'll cause the configuration system to ignore all connection strings added in machine.config and use the new ones provided. You can also use <remove> element to remove a single configuration item if you don't want to clear out the whole thing.

<connectionStrings>
   <clear />
   <add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/> 
</connectionStrings>

The same idea applies to <providers> sections as well.

like image 73
mmx Avatar answered Sep 18 '22 12:09

mmx