create your own settings in xml

后端 未结 3 1600
渐次进展
渐次进展 2020-12-06 08:20

I\'m in a ASP.NET project where I need to give several parameters to the administrator that is going to install the website, like:

AllowUserToChangePanelLayo         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 09:18

    just to let you guys know that I did what configurator recommended but with a twist.

    instead of asking all the time (that I need) for

    System.Configuration.ConfigurationManager.AppSettings["myKey"];
    

    I just created a static class that would pull this values with what we call by Strongly typed values (so you don't need to remember all the values)

    the mySettings class

    public static class mySettings
    {
        public enum SettingsType
        { UserPermitions, WebService, Alerts }
        public enum SectionType
        { AllowChangeLayout, AllowUserDelete, MaximumReturnsFromSearch, MaximumOnBatch, SendTo }
    
        public static String GetSettings(SettingsType type, SectionType section)
        {
            return
                ConfigurationManager.AppSettings[
                    String.Format("{0}_{1}",
                        Enum.Parse(typeof(SettingsType), type.ToString()).ToString(),
                        Enum.Parse(typeof(SectionType), section.ToString()).ToString())
                ];
        }
    }
    

    the web.config appSettings part

    
      
        
            
    
         
    
        
        
      
    
    

    the entire myApp.config file

    
    
    
    
      
      
      
      
      
    
      
      
      
    
      
      
      
      
      
    
    
    

    So, now I call like this:

    p.value = mySettings.GetSettings(
                 mySettings.SettingsType.WebService, 
                 mySettings.SectionType.MaximumReturnsFromSearch);
    

    Hope that helps someone with the same problem :)

提交回复
热议问题