More in this section

Forums / Developing with Sitefinity / No reading web.config help

No reading web.config help

2 posts, 0 answered
  1. Pierre
    Pierre avatar
    433 posts
    Registered:
    16 Feb 2006
    06 Nov 2008
    Link to this post
    Hi,
    I try to read some elements in web.config with no issue in v35sp1 and working well in v31sp1. All classes and using elem are well added and ok. System.configuration is referenced in classes and in web application but I'm not able to read my web.config. 

    I have added one section in web config and the keys like

    web.config
    <section name="GeneralSettings" type="GeneralSettings" allowDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="false"/>

    and the key to read
    <GeneralSettings loginRequirement="somevalue"/>

    Now I have one page that demand in codebehind my value in web.config
    ddl.SelectedValue = SiteConfig.RequireLogin;

    And the classes in App_Code that respond
    SiteConfig.cs

    public static class SiteConfig
    {
        static GeneralSettings generalSettings;
        public static void Load()
        {
            // Get the current configuration file.
            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            generalSettings = (GeneralSettings)config.GetSection("GeneralSettings");
        }

        #region Publics
        public static string RequireLogin
        {
            get { return generalSettings.LoginRequirement; } 
     //THIS LINE GENERATE THE ERROR
         }
        #endregion
    }

    And The General Settings class
    public class GeneralSettings:ConfigurationSection
    {
       
        [StringValidator(MinLength = 1)]
        [ConfigurationProperty("loginRequirement",
           DefaultValue = "myvalue")]
        public string LoginRequirement
        {
            get { return (string)base["loginRequirement"]; }
            set { base["loginRequirement"] = value; }
        }
       
    }

    Some problem or limitation to read web.config is enable ?

    Helps  Welcome. Thanks
  2. Ivan
    Ivan avatar
    478 posts
    Registered:
    16 Jun 2015
    10 Nov 2008
    Link to this post
    Hello Pierre,

    there are no limitations on the reading of web.config file (we are using it to read the settings of providers and other things). Please read the following article on how to create custom web.config sections (which is what you are doing):
    http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

    I hope you'll find this information helpful. Let us know if there is anything else we can do for you.

    Best wishes,
    Ivan
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
2 posts, 0 answered