ASP.NET Hosting

Using configSource to split configuration files

As Nikhil Kothari writes in his blog, he's started to use the configSource attribute to split configuration files into smaller pieces.

For example, here is how he exports the profile configuration into a dedicated configuration file.
This is web.config:

    <profile configSource="profile.config" />

This is profile.config:

    <add name="Name" type="String" />
    <add name="Age" type="Int32" />

Make sure you use .config as the extension of your files so they cannot be served to the browser. Avoid .xml for example or your files can be available to prying eyes.

I've been using this feature for my web sites for a while. This allows having a unique web.config file, and separate sub-files for things that are different between the development machine and the hosting environment. For example, I have different connectionStrings.config and smtp.config files for the two environments. The benefits: the web.config file is smaller and hence easier to read, and you don't need a complete web.config file for each environment.


  • Fabrice you might also use Visual Studio 2005 Web Deployment Projects to manage build configurations that are stage dependant. I am using it on Tech Head Brothers website for example.

  • Now if only you could point configSource at a parent directory it might be useful. Quite ridiculous that it doesn't support absolute paths or at least ..\ notation...

  • So how do I reference this in my code-behinds?

    I am creating a web app and using AppSettings which may be modified by the administrator of the application, but I really want to avoid allowing them to have access to the entire web.config file.

    It doesn't look like I can subscribe to this post. &nbsp;Could you maybe send me an email to matt [dot] schutz [at] gmail [dot] com?


  • Matt, you reference the configuration settings in code in the same way you'd reference them if they were all in a single file (web.config or yourapp.exe.config). This approach only gives you the option of splitting configuration files, but other than that it's transparent from the code's point of view.

  • I have discovered something. &nbsp;I'm not sure if it's a different .NET framework version or just different for appSettings or what, but "file" is the correct attribute for appSettings for me.



    ... &nbsp;

    &lt;appSettings file="setup.config" /&gt;







    Note that setup.config root is "appSettings", not "configuration".

    Hope this helps someone else! &nbsp;Thanks for the help.

  • Could you show a sample for connectionstrings in app.config 2008. I am having a beastly problem getting this simple thing to work. I keep getting "connection string not initialized. The extension, the path of the connSstrings.config iscorrect. Does one need to add anything other than this to the app.config? Or do something other than build the project.

    Will the program be able to access the string in the same fashion as when it is physically in the app.config?

  • I don't do anything more than what you did.
    Does your connStrings.config file correctly starts with a tag?
    Are you sure that this file is correctly deployed in the output directory, where your executable is?

  • I discovered finally the answer to the problem. The property "Copy to Out put Directory" of the configsource file must be set to "copy always"

    I found this at BlueSam Blog.

    Now another question I have not yet found an answer to: will this work with User and Application settings. I tried it and I receive xml errors. Either this is impossible or I do not have the correct section head.

  • I am doing the following code
    Changes in web.config file

    Creating a new file "ConnectionString.config"
    put the following code in the new file

    on my code behind page i am writing the following code
    Configuration config = Configuration.WebConfigurationManager.OpenWebConfiguration("~/ConnectionString.config");
    string connString = config.ConnectionStrings.ConnectionStrings[0].ConnectionString;
    DataSet ds = new DataSet();
    SqlConnection sql = new SqlConnection();
    SqlDataAdapter adp = new SqlDataAdapter("select * from central_audit", sql);
    catch (Exception ex)
    throw ex;
    return ds;

    and facing the error
    "The format of a configSource file must be an element containing the name of the section. " :(
    Please help me whr i am going wrong

  • Rajul,

    You don't have to use OpenWebConfiguration. You should be able to use System.Configuration.ConfigurationManager.ConnectionStrings directly.
    You'd use OpenWebConfiguration when you want to load a configuration file other than the default one (Web.config), but it has to be in the same format as Web.config. It cannot be used to load partial configuration files such as your ConnectionString.config file.

    Also, don't forget to use your connString variable as a parameter of the SqlConnection constructor.

    At the same time, please get rid of the useless try..catch block. I hate it when I see code crippled with this kind of thing. You can use Visual Studio's debugger window or evaluate $exception to see the detail of the current exception.

  • Hi,

    I just wanted to say thanks for this tip. It just saved me a lot of duplication between my DLL and App config files :)

  • You're welcome.

  • Hi,
    Thanks but I'm getting the same error as Rajul.
    I am using:
    ConnectionStringSettings connectionString = ConfigurationManager.ConnectionStrings["Northwind"];

    to retrieve the value so it should work, right?

    So why do i receive the error?

    Thanks for a great article. Love to get this going.

  • @nt:

    I'm using:

    string sqlCn = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnString"].ConnectionString;

    Where my web.config looks like:

    Where my db.config looks like:

Comments have been disabled for this content.