09/06
This commit is contained in:
@ -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("");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
else
|
||||
{
|
||||
t.Wait();
|
||||
}
|
||||
return list;
|
||||
await t;
|
||||
}
|
||||
}
|
||||
return new List<NavItem>();
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user