views:

354

answers:

3

I have a project for which we are extending some functionality from an existing client into a web portal in ASP.NET 2.0. The client is windows forms based (in .NET 2.0). It has some settings that are kept in the Project > Properties > Settings.settings system, including a generated settings.Designer.cs file. This file provides nice automatic wrappers for each of the settings.

In trying to set up the website I have been frustrated by an apparent lack of parity for this feature. I have a web.config, and it can have an section. This means access via code with strings, for example:

WebConfigurationManager.AppSettings["mySetting"];

I can even have the settings refer to another file this way, affording a little abstraction, and easier check-ins to source control:

<appSettings configSource="web.settings.config"/>

but ultimately this lacks some of the functionality of the client projects settings system.

Particularly I would very much like these features if at all possible:

  • Autogenerated accessor class (for convenience, intellisense..)
    • Convenient
    • Strongly Typed
    • Provides Intellisense
    • Code will not compile against typos/mistakes in settings names
  • Easy Interface
    • The settings.Settings before provided a nice grid
    • All options represented
    • Showed dropdown choices for certain options
    • Don't have to edit XML
    • Can't fat-finger an angle bracket

I know that it would be possible to create a class that wrapped these, but it would not stay in sync with the settings automatically, and would have to be manually edited on any change. It would provide some of the above however.

Can I get parity with the project settings like we do in our client?

What is the best way to manage settings for an ASP.NET 2.0 website?

+1  A: 

You should use Web Application, not Website and you'll have access to settings in the same way as Windows applications

Albert
I'm not certain that is an option for us, am I out of luck with a web site?
Colin Dabritz
You're not totally out of luck, but for a website you have to "manually" parse the settings from the config file.
Albert
A: 

By default, using the appSettings section in your web.config is the simplest way to manage your application settings for the web application. You can cast the values within the application settings as needed. However, you will lose strong typing in this case, which sounds like a deal breaker to you.

If you do need to retain the strong typing, go into the project settings (similar to the WinForms application) and set your values there. You'll notice that settings.settings files exists within a MyProjects folder, but you'll also find the following code snippet down at the bottom of your web.config file (swiped from one of my applications):

<applicationSettings>
   <EAF.My.MySettings>
      <setting name="EAF_AS400EmployeeServices_EmployeeServices" serializeAs="String">                                          
      <value>http://countynetappsdev/AS400DataService/EmployeeServices.asmx&lt;/value&gt;
  </setting>
</applicationSettings>

Notice the serializeAs property in this section. Once you have things in this section, you can access the settings through the My.Settings library using VB.NET. However, since you're using C#, you need a smidge more work, but a simple code solution can be found here.

Dillie-O
A: 

I create classes that wrap my web.config to provide strongly typed, intellisense friendly access to the settings.

Also, the settings are most likely to be changed in the XML by the support guys at implementation time, so the grid in VS makes no difference. Granted, its not auto generated, but copy/paste is your friend :)

ck