开发者

Reading & Editing configuration inside Web.config

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 i开发者_如何学JAVAnside 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.


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, ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜