/// <summary> /// 默认页面 /// </summary> public ActionResult Index() { WebConfigVM model = new WebConfigVM(); #region 遍历进行赋值 /* 思考对比之后,最终调整为:反射方式去获取数值。 * 后续只需要维护[WebConfigVM]类即可,不用再硬编码相关的读取和保存语句。 */ foreach (var fieldInfo in typeof(WebConfigVM).GetProperties()) { //获得Raw值 string rawValue = EG.Business.Common.ConfigCache.GetAppConfig(fieldInfo.Name); if (rawValue == null) { Logger.Log4Net.Warn(String.Format("WebConfigController Load config missing data,Key name is [{0}]", fieldInfo.Name)); continue; } Type targetType = fieldInfo.PropertyType; if (targetType == typeof(String)) { //##字符串,直接赋值 fieldInfo.SetValue(model, rawValue); } else if (targetType.IsEnum) { //##枚举值,尝试转换找到合适的值 object targetEnum = null; var converter = TypeDescriptor.GetConverter(targetType); if (converter != null) { try { targetEnum = converter.ConvertFromString(rawValue); } catch { targetEnum = null; } } //##最终处理 if (targetEnum != null) { fieldInfo.SetValue(model, targetEnum); } else { Logger.Log4Net.Warn(String.Format("WebConfigController Load config missing data,Key name is [{0}],Type is [{1}] and value is [{2}]", fieldInfo.Name, targetType, rawValue)); continue; } } } #endregion return(View(model)); }
/// <summary> /// 保存配置 /// </summary> /// <param name="model">数据</param> public ActionResult SaveConfig(WebConfigVM model) { try { //获取userKey string userKey = EG.Business.Common.ConfigCache.GetAppConfig("UserKey"); //获取配置文件 string configPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; XDocument xdoc = XDocument.Load(configPath); XElement xeAppsetting = xdoc.Root.Element("appSettings"); foreach (var fieldInfo in typeof(WebConfigVM).GetProperties()) { //获取是否需要加密 bool IsNeedEncrypt = false; { EncryptFieldAttribute ef = Attribute.GetCustomAttribute(fieldInfo, typeof(EncryptFieldAttribute)) as EncryptFieldAttribute; if (ef != null) { IsNeedEncrypt = ef.NeedEncrypt; } } //获取数值 string value = ""; var rawValue = fieldInfo.GetValue(model); if (rawValue is string) { value = rawValue as string; } else if (rawValue is Enum) { value = rawValue.GetHashCode().ToString(); } //密码类型,如果空白则保持不变 { DataTypeAttribute da = Attribute.GetCustomAttribute(fieldInfo, typeof(DataTypeAttribute)) as DataTypeAttribute; if (da != null && da.DataType == DataType.Password && String.IsNullOrEmpty(value)) { value = EG.Business.Common.ConfigCache.GetAppConfig(fieldInfo.Name); } } //写入 WriteValue(xeAppsetting, fieldInfo.Name, value, IsNeedEncrypt ? userKey : null); } //最终一次性写入 xdoc.Save(configPath); //返回结果 return(Json(new { IsSuccess = true })); } catch (Exception ex) { //返回结果 return(Json(new { IsSuccess = false, Message = ex.Message })); } }