public void EditApiSettings(ApiSettings settings) { foreach (var setting in settings) { switch (setting.SettingType) { case ApiSettingType.AppSettings: Configuration.AppSettings.Settings[setting.Key].Value = setting.Value; Configuration.Save(ConfigurationSaveMode.Modified); break; case ApiSettingType.CustomErrors: var customErrorsSection = (CustomErrorsSection)Configuration.GetSection("system.web/customErrors"); if (setting.Key == "Mode") { customErrorsSection.Mode = (CustomErrorsMode)Enum.Parse(typeof(CustomErrorsMode), setting.Value); } else if (setting.Key == "DefaultRedirect") { customErrorsSection.DefaultRedirect = setting.Value; } else if (setting.Key == "500Error") { if (string.IsNullOrEmpty(setting.Value)) { customErrorsSection.Errors.Remove("500"); } else { if (customErrorsSection.Errors["500"] == null) { customErrorsSection.Errors.Add(new CustomError(500, setting.Value)); } else { customErrorsSection.Errors["500"].Redirect = setting.Value; } } } Configuration.Save(ConfigurationSaveMode.Modified); break; case ApiSettingType.ClientDependency: ClientDependency.Descendants("clientDependency").Attributes().Single(x => x.Name == "version").Value = setting.Value; ClientDependency.Save(HttpContext.Current.Server.MapPath(CLIENT_DEPENDENCY_PATH)); break; case ApiSettingType.UmbracoConfig: if (setting.Key == "error404") { UmbracoSettings.Descendants("error404").First().Value = setting.Value; } else if (setting.Key == "loginBackgroundImage") { UmbracoSettings.Descendants("loginBackgroundImage").First().Value = setting.Value; } else if (setting.Key == "email") { UmbracoSettings.Descendants("email").First().Value = setting.Value; } UmbracoSettings.Save(HttpContext.Current.Server.MapPath(UMBRACO_SETTINGS_PATH)); break; default: break; } } settings.LoadConfigurationsIntoMemory(); }
//[Obsolete] //public (string, string) GetApiSettings() //{ // AddApiSettings(); // return (configuration.AppSettings.Settings["TotalCode.Admin.AppId"].Value, configuration.AppSettings.Settings["TotalCode.Admin.ApiKey"].Value); //} public ApiSettings GetApiSettings() { var settings = new ApiSettings(); // web.config settings var keys = Configuration.AppSettings.Settings.AllKeys.Where(x => x.Contains("TotalCode.Admin.")); foreach (var key in keys) { settings.Add(new ApiSetting { Key = key, Value = Configuration.AppSettings.Settings[key].Value, SettingType = ApiSettingType.AppSettings }); } var customErrorsSection = (CustomErrorsSection)Configuration.GetSection( "system.web/customErrors"); var customErrorsMode = new ApiSetting { Key = "Mode", Value = customErrorsSection.Mode.ToString(), SettingType = ApiSettingType.CustomErrors }; var customErrorsDefaultRedirect = new ApiSetting { Key = "DefaultRedirect", Value = customErrorsSection.DefaultRedirect, SettingType = ApiSettingType.CustomErrors }; var customErrors500Error = new ApiSetting { Key = "500Error", Value = customErrorsSection.Errors["500"] != null ? customErrorsSection.Errors["500"].Redirect : "", SettingType = ApiSettingType.CustomErrors }; settings.Add(customErrorsMode); settings.Add(customErrorsDefaultRedirect); settings.Add(customErrors500Error); // Get Umbraco settings var error404 = new ApiSetting { Key = "error404", Value = UmbracoSettings.Descendants("error404").First().Value, SettingType = ApiSettingType.UmbracoConfig }; var notificationEmail = new ApiSetting { Key = "email", Value = UmbracoSettings.Descendants("email").First().Value, SettingType = ApiSettingType.UmbracoConfig }; var loginBackgroundImage = new ApiSetting { Key = "loginBackgroundImage", Value = UmbracoSettings.Descendants("loginBackgroundImage").First().Value, SettingType = ApiSettingType.UmbracoConfig }; // Client dependency Settings var clientDepenency = new ApiSetting { Key = "clientDependency", Value = ClientDependency.Descendants("clientDependency").Attributes().Single(x => x.Name == "version").Value, SettingType = ApiSettingType.ClientDependency }; settings.Add(error404); settings.Add(notificationEmail); settings.Add(loginBackgroundImage); settings.Add(clientDepenency); return(settings); }