Use linked configuration files in your App.Config and Web.config to clean up that mess

If you are like me, (and the government hasn’t abducted you yet), then you hate those app.config’s that are 2,000 lines long and just messy as hell. What to do….oh what to do….

Well, one thing that is a great little gem of .NET 2.0 and above is the ability to put each of your sections in their own file. Sounds too good to be true, right? I go to the connection strings file, and just see …Connection strings. Not wading through the first 200 lines of crap to find the little section I want to work with.

So, how does one take advantage of this little documented trick? Super easy. The magical configSource attribute.  Why isn’t this easily found in the docs, no idea. All I know is that it works.

So, lets say you have your messy web.config and turn it into this:


<?xml  version="1.0" encoding="utf-8"?>
.......blah blah
<connectionStrings  configSource="connectionStrings.config"  />


<?xml  version="1.0" encoding="utf-8"?>
<add  name="SeekfordDB"  connectionString="mystical,magical connection string here" providerName="System.Data.SqlClient"  />

You can do this for most of the sections. Now your web.config and app.config are nice little street signs to point to the real meat. And you have all your data in a nice easy to read modular fashion.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *