Friday, November 11, 2011

Add Customization to Web.config

I have not touched ASP.NET project for about one year.  Now I am back to a new ASP.NET project. Even though it is not too hard to pick it up and add my new skills and extensions to the project, I have to constantly refer to the project I worked before.  For sure, I would not like to copy the same structure and design into the new project.  The project is a .NET 2.0 based ASP.NET, but I am planning to update it to .NET 4.0 ASP.NET MVC 3 in the next stage, most likely next month or next year, if my contract extension would pass.

One thing I like to add is customized configuration.  I prefer to define my configuration out side of web.config, because there are so many special nodes designated to ASP.NET project or web site. I know there is a way to add customized nodes there, but I want to re-use my pattern to parse XML doc with my library.

At least I have to add one node in web.config. This is very easy:

<configuration>
  <!-- application specific settings -->
  <appSettings>
    <add key="MyConfigurationFile" value="config/MyConfiguration.xml" />
  </appSettings>
  ...
</configuration>

The above entry defines an application-wide setting named MyConfigurationFile with the string value config/MyConfiguration.xml, the configuration xml file in the web site folder config. With this setting, I can get the xml file like:

string file = ConfigurationSettings.AppSettings("MyConnfigureationFile");
file = string.Format("{0}{1}",
    AppDomain.CurrentDomain.BaseDirectory, file);
...

Reference

0 comments: