Friday, 22 June 2007

Factoring Web.Config Configuration

There are many things which are there, but you didn’t quite know existed!
You might not have heard of a little known attribute called configSource, which can be specified on a section which allows the definition of the section to live in another actual file.
Consider the profile feature that allows you to specify a bunch of named properties that should be managed per-user, along with type information and other metadata. This information is currently written within the <profile> section within <system.web> in web.config.
This always sounds me odd. I have always thought it would be better if this information was in a separate .profile kind of file. Well, with configSource, you can do just that...
For example, see how can I use it into my web.config:
...
<system.web>
...
<profile configSource="profile.config" />
...
</system.web>
...
Once I've done that, I can now add a profile.config file into my web site as follows:
<profile>
<properties>
<add name="Name" type="String" />
<add name="Age" type="Int32" />
</properties>
</profile>

Everything continues to work as before. Essentially I've cut out the actual profile section from web.config and moved it into a separate file, which might help manage config a bit better, simply by splitting it out, as well as perhaps make me a bit happier that profile information isn't mixed with configuration! Note that you should probably still name these additional files as .config, so they aren't served out.

kick it on DotNetKicks.com

No comments:

Post a Comment