web.config configures custom node value format to support key-value pair data

web.config configuration

<configuration> 
  <appSettings> 
    <!-- 系统角色配置,格式参考(500#部门管理员12A)(900#超级管理员B2b)(1200#管理员甲B2b) -->
    <add key="rolesCofing" value="(500#部门管理员)(900#超级管理员)" />
	</appSettings> 
</configuration> 

Actual effect:
key=500,value=department administrator
key=900,value=super administrator

Read configuration

    /// <summary>
    /// 初始化程序数据
    /// </summary>
    public static class InitData
    {
    
    
        /// <summary>
        /// 角色列表
        /// </summary>
        public static Dictionary<int, string> RoleDict = new Dictionary<int, string>();
		
		  static InitData()
        {
    
    
            string roleStr = System.Configuration.ConfigurationManager.AppSettings["rolesCofing"];
            Regex regex = new Regex("(\\d+#[\u4e00-\u9fa5]+[A-Za-z0-9]*)");
            MatchCollection matchCollection = regex.Matches(roleStr);
            if (matchCollection.Count == 0)
            {
    
    
                LogHelpter.AddLog("角色初始化数据,没有匹配到角色项,请确认web.config中<appSettings>配置了<add key=\"rolesCofing\" value=\"(500#部门管理员)(900#超级管理员)\"/>");
            }
            try
            {
    
    
                foreach (Match item in matchCollection)
                {
    
    
                    int roleInt = Convert.ToInt32(item.Value.Split('#')[0]);
                    string roleName = item.Value.Split('#')[1];
                    RoleDict.Add(roleInt, roleName);
                }
            }
            catch (Exception ex)
            {
    
    
                LogHelpter.AddLog("角色初始化数据异常," + ex.ToString());
            }

        }

    }


Guess you like

Origin blog.csdn.net/u011511086/article/details/111171574