This commit is contained in:
2025-06-09 09:50:54 +07:00
parent 5beb66f11a
commit 1e635f53bf
3 changed files with 47 additions and 23 deletions

View File

@ -1,4 +1,6 @@
using Newtonsoft.Json;
using Microsoft.AspNetCore.Html;
using Microsoft.Extensions.Logging.Console;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
@ -23,8 +25,9 @@ namespace AppLibs.Libs
[JsonProperty(PropertyName = "url")]
public string Url { set; get; }
[JsonProperty(PropertyName = "flexPage")]
public bool IsFlexPage { set; get; }
[JsonProperty(PropertyName = "flexpage")]
public bool? IsFlexPage { set; get; }
[JsonProperty(PropertyName = "sub-item")]
public List<NavItem> SubItem { set; get; }
@ -40,5 +43,19 @@ namespace AppLibs.Libs
SubItem = new List<NavItem>();
}
public IHtmlContent ToFlexPageAttribute()
{
Console.WriteLine(IsFlexPage.ToString());
if (IsFlexPage.HasValue && IsFlexPage.Value)
{
return new HtmlString("IsFlexPage");
}
else
{
return new HtmlString("");
}
}
}
}

View File

@ -9,40 +9,47 @@ namespace AppLibs.Libs
{
public class WSNavigation
{
private static List<NavItem> list;
private static List<NavItem> list = new List<NavItem>() ;
private static readonly object loadLock = new object();
private static Task? t;
private static Task t;
public static void LoadJson()
public static async Task LoadJson()
{
t = Task.Run(() => LoadNavItem());
lock (loadLock)
{
// Nếu đang có task load cũ chưa xong, có thể đợi hoặc hủy (tùy logic)
if (t != null && !t.IsCompleted)
return;
t = LoadNavItem();
}
await t.ConfigureAwait(false);
}
static async Task LoadNavItem()
{
list = new List<NavItem>();
var newList = new List<NavItem>();
using (var reader = new StreamReader(Path.GetFullPath("Json/navlist.json")))
{
string json = await reader.ReadToEndAsync();
list = JsonConvert.DeserializeObject<List<NavItem>>(json);
string json = await reader.ReadToEndAsync().ConfigureAwait(false);
var deserializedList = JsonConvert.DeserializeObject<List<NavItem>>(json);
if (deserializedList != null)
{
newList = deserializedList;
}
}
System.Threading.Interlocked.Exchange(ref list, newList);
}
public static List<NavItem> GetListMenu()
public static async Task<List<NavItem>> GetListMenu()
{
if (t != null)
{
if (t.IsCompleted)
if (!t.IsCompleted)
{
return list;
await t;
}
else
{
t.Wait();
}
return list;
}
return new List<NavItem>();
return list;
}
}
}

View File

@ -3,7 +3,7 @@ using AppLibs.Libs;
using ManagementApp.Models.Services;
using Microsoft.AspNetCore.Server.Kestrel.Core;
WSNavigation.LoadJson();
await WSNavigation.LoadJson().ConfigureAwait(false);
var builder = WebApplication.CreateBuilder(args);