3

I have a few settings that need to be edited from the admin panel of the site I'm currently working on. I thought it makes sense to place those settings inside the web.config (or should I place them somewhere else?). So anyway, I'm trying to write the necessary code to do this, but I got stuck... This is the first time I actually needed to do this so... :) Here's what I've got so far:

appSettings section inside the Web.config:

  <appSettings>
    <add key="ClientValidationEnabled" value="true" /> 
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="InvitationOnly" value="true" />
    <add key="MaintainanceMode" value ="false"/>
  </appSettings>

And here's a class I'm trying to write to allow for easy retrieval and modification of some values that will be placed inside the appSettings section:

public static class SiteSettings
{
    public static bool InvitationOnly
    {
        get
        {
            var invitation = WebConfigurationManager.AppSettings["InvitationOnly"];
            return Convert.ToBoolean(invitation);
        } 
        set
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            var appSettings = config.GetSection("appSettings") as AppSettingsSection;
            if(appSettings != null)
            {
                //got stuck here...
            }
        }
    }
}

Am I on the right track? How do I continue from here?

And by the way, how safe is it to place site settings inside the web.config? Should I be worried about anything?

Thank you.

Kassem
  • 8,116
  • 17
  • 75
  • 116

1 Answers1

7

You definitely don't want to modify the web.config file as every time you do so your application will simply restart. If the web.config is modified ASP.NET immediately unloads the AppDomain. The web.config is used to store static, readonly configuration of the application. If you need to store settings that has to be modified, web.config is not the right place. You could use a custom file, session, cookies, a database, ...

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yeah I've read somewhere that editing the web.config file restarts the application, but what does that mean exactly? What do you mean by unloading the AppDomain? And finally, I guess it's best to store this data in an XML file I believe? – Kassem May 04 '11 at 16:43
  • @Kassem, it means that the application is unloaded from memory and all requests aborted. It means that whatever you might have stored in memory is lost and the Application_Start event will be hit on subsequent request. As far as what is *best* is difficult to say as you haven't provided enough details about your scenario. Just note that if you go the XML file route, don't forget to synchronize the access to this file as while an administrator is writing to it if some user tries to read at the same time you might get into troubles. – Darin Dimitrov May 04 '11 at 16:46
  • Oh I definitely wouldn't want that to happen. Thanks for the warning! About the XML file... only the admin will be able to write to this file. But yes, users will be reading from that file. Any hint of what I need to be researching exactly? – Kassem May 04 '11 at 16:51