Settings Final Fix
Settings Not Persisting - Final Fix
Issues Fixed
- Wrong appsettings.json path: API might have been saving to wrong file location
- Stale data after save: UI was reloading from API immediately after save, getting old values
- Missing logging: No visibility into what was happening
Root Cause
The issue had two parts:
1. File Path Issue
The API was using AppContext.BaseDirectory which might point to the wrong location (build output vs source directory). Now it tries multiple paths:
var possiblePaths = new[]
{
Path.Combine(_environment.ContentRootPath, "appsettings.json"), // Most reliable
Path.Combine(AppContext.BaseDirectory, "appsettings.json"),
Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json")
};
2. Stale Data After Save
The UI was calling await LoadSettings() immediately after saving, which would:
- Send request to API
- Get values from
IOptionsMonitorwhich hadn't reloaded yet - Return old values (30, 10)
- UI displays old values
Fix: Don't reload - keep the values that were just saved.
Changes Made
BackupController.cs
- Added
IWebHostEnvironmentto find correct content root - Added multiple path checking for appsettings.json
- Added extensive logging for debugging
- Logs before/after save to verify values
// Log what we're saving
_logger.LogInformation("UpdateConfiguration called: Freq={Freq}, Dir={Dir}, Max={Max}");
// Try multiple paths
var possiblePaths = new[] { ... };
// Log after reload to verify
_logger.LogInformation("After reload: Freq={Freq}, Dir={Dir}, Max={Max}");
BackupConfig.razor
Removed the LoadSettings() call after save:
Before:
if (success)
{
SetStatus("Settings saved!");
await Task.Delay(500);
await LoadSettings(); // This was getting stale data!
}
After:
if (success)
{
// Keep the values we just saved (don't reload)
frequencyMinutes = savedFrequency;
backupDirectory = savedDirectory;
maxBackupsToKeep = savedMaxBackups;
lastLoadTime = DateTime.Now;
SetStatus("Settings saved!");
}
BedrockApiService.cs
Updated to return saved config in response:
public async Task<(bool success, string message, BackupConfigResponse savedConfig)>
UpdateBackupConfigAsync(...)
Testing
Check API Logs
When you save, you should see in the API logs:
[BackupController] UpdateConfiguration called: Freq=60, Dir=C:\Backups, Max=5
[BackupController] Checking path: C:\...\MCBDS.API\appsettings.json, Exists: True
[BackupController] Using appsettings.json at: C:\...\MCBDS.API\appsettings.json
[BackupController] Writing to ...: {"Logging":...,"Backup":{"FrequencyMinutes":60,...}}
[BackupController] Backup configuration saved to file
[BackupController] Configuration reloaded
[BackupController] After reload: Freq=60, Dir=C:\Backups, Max=5
Check UI Console
[BackupConfig] Saving: Freq=60, Dir=C:\Backups, Max=5
[BackupConfig] Save successful
Verify appsettings.json
After saving, check the file:
{
"Backup": {
"FrequencyMinutes": 60, Should be your new value
"BackupDirectory": "C:\\Backups",
"MaxBackupsToKeep": 5
}
}
Before vs After
Before (Buggy)
1. User sets frequency to 60
2. Clicks Save
3. API saves to file
4. UI calls LoadSettings()
5. API returns 30 (IOptionsMonitor hasn't reloaded)
6. UI shows 30
After (Fixed)
1. User sets frequency to 60
2. Clicks Save
3. API saves to file
4. UI keeps value 60 (doesn't reload)
5. UI shows 60
If Still Not Working
Check 1: API Logs
Look for these log messages:
- "UpdateConfiguration called" - Was the request received
- "Using appsettings.json at" - Which file is being updated
- "Backup configuration saved to file" - Did the save succeed
- "After reload" - What values are loaded after reload
Check 2: File Location
The API response now includes savedTo field:
{
"message": "Configuration updated successfully!",
"savedTo": "C:\\Users\\joshua\\source\\repos\\...\\MCBDS.API\\appsettings.json"
}
Verify this is the correct file.
Check 3: File Contents
Open the file at savedTo path and verify the values are actually updated.
Check 4: Permissions
Make sure the API has write access to appsettings.json.
Summary
The fix ensures:
- API finds the correct appsettings.json file
- API logs what it's doing for debugging
- UI keeps saved values instead of reloading stale data
- Clear error messages if something goes wrong
Status
Build: Successful
Path Resolution: Fixed
Stale Data: Fixed
Logging: Added
Ready for: Testing
Run the API and check the logs when you save settings. The logs will tell you exactly what's happening!