diff --git a/AppLibs/AppLibs/Libs/NavItem.cs b/AppLibs/AppLibs/Libs/NavItem.cs index 393b2f1..ed337c3 100644 --- a/AppLibs/AppLibs/Libs/NavItem.cs +++ b/AppLibs/AppLibs/Libs/NavItem.cs @@ -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 SubItem { set; get; } @@ -40,5 +43,19 @@ namespace AppLibs.Libs SubItem = new List(); } + public IHtmlContent ToFlexPageAttribute() + { + Console.WriteLine(IsFlexPage.ToString()); + if (IsFlexPage.HasValue && IsFlexPage.Value) + { + + return new HtmlString("IsFlexPage"); + } + else + { + return new HtmlString(""); + } + } + } } diff --git a/AppLibs/AppLibs/Libs/WSNavigation.cs b/AppLibs/AppLibs/Libs/WSNavigation.cs index 425f878..096bb72 100644 --- a/AppLibs/AppLibs/Libs/WSNavigation.cs +++ b/AppLibs/AppLibs/Libs/WSNavigation.cs @@ -9,40 +9,47 @@ namespace AppLibs.Libs { public class WSNavigation { - private static List list; + private static List list = new List() ; + 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(); + var newList = new List(); using (var reader = new StreamReader(Path.GetFullPath("Json/navlist.json"))) { - string json = await reader.ReadToEndAsync(); - list = JsonConvert.DeserializeObject>(json); + string json = await reader.ReadToEndAsync().ConfigureAwait(false); + var deserializedList = JsonConvert.DeserializeObject>(json); + if (deserializedList != null) + { + newList = deserializedList; + } } + System.Threading.Interlocked.Exchange(ref list, newList); } - public static List GetListMenu() + public static async Task> GetListMenu() { if (t != null) { - if (t.IsCompleted) + if (!t.IsCompleted) { - return list; - } - else - { - t.Wait(); - } - return list; + await t; + } } - return new List(); - + return list; } } } diff --git a/ManagementApp/Program.cs b/ManagementApp/Program.cs index 0767499..94ffb00 100644 --- a/ManagementApp/Program.cs +++ b/ManagementApp/Program.cs @@ -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);