Вот соответствующий фрагмент кода:
Код: Выделить всё
private static Dictionary _latestImages;
< /code>
HomeController.cs:
[HttpGet]
public JsonResult GetLatestImages()
{
lock (_fileLock)
{
try
{
var jsonResult = Json(_latestImages);
Console.WriteLine("JSON response successfully prepared.");
return jsonResult;
}
catch (Exception ex)
{
Console.WriteLine($"Error while preparing JSON: {ex.Message}");
return Json(new { Error = "Error while preparing JSON." });
}
}
}
< /code>
This GetLatestImagesКод: Выделить всё
index.cshtml:
function updateImages() {
$.getJSON('/Home/GetLatestImages', function (data) {
const currentTime = new Date().getTime();
for (const projectKey in data) {
if (!lastUpdatedTimes[projectKey]) {
lastUpdatedTimes[projectKey] = {};
}
const project = data[projectKey];
for (const folderKey in project) {
const folder = project[folderKey];
const imageBox = $(`#image-${projectKey}-${folderKey}`);
const imgTag = imageBox.find('img');
const dateTag = imageBox.find('.date');
if (folder.LocalFilePath) {
const newImageSrc = `${folder.LocalFilePath}?t=${new Date().getTime()}`;
if (imgTag.attr('src') !== newImageSrc) {
lastUpdatedTimes[projectKey][folderKey] = currentTime;
imgTag.attr('src', newImageSrc).attr('alt', 'Latest Image');
dateTag.text(`Last Taken: ${new Date(folder.LastModified).toLocaleString()}`);
}
} else {
imgTag.attr('src', '').attr('alt', '');
dateTag.text('Image Pending..');
}
}
}
}).fail(function () {
console.error("Failed to load JSON data.");
});
}
< /code>
The AJAX call fetches the data every 10 seconds and updates the images in the UI. But since the JSON response is empty, the images are never updated.
Debugging Steps I Tried
[list]
[*]Checked the _latestImagesКод: Выделить всё
StartImageUpdateLoop[*]Проверено, что метод GetLatestImages активирован. и выполняется успешно.
[/list]
Вопросы:
- Почему ответ Json(_latestImages) пуст, когда _latestImages заполнен?
- Что-то не так с использованием блокировки или со способом сериализации JSON?
Может ли это быть проблема с кэшированием, и если да, то как я могу ее отладить или решить?
< /li>
Подробнее здесь: https://stackoverflow.com/questions/793 ... t-core-mvc
Мобильная версия