我有一個 Web 應用程式,其中包含一列名為active. 如果一行不活動,該行仍會呈現給用戶。
客戶希望有一個localStorage切換選項來隱藏視圖中的非活動行。由于我的所有邏輯都發生在控制器中,使用localStorage過濾 html 行似乎不合適。(使用 javascript 隱藏元素與簡單地在控制器級別過濾資料)。
如果我無法在資料庫中為“隱藏非活動專案”功能添加一個新列,那么toggle inactive items在整個應用程式中添加一個新列的最佳解決方案是什么?
我正在考慮創建一個索賠:
new Claim(ClaimTypes.UserData, "true", ClaimValueTypes.Boolean), // Hide inactive rows
稍后在應用程式中:
internal static bool showInactiveRows(ClaimsPrincipal user)
{
var showInactive = true; // Default hide inactive rows
var identity = (ClaimsIdentity)user.Identity;
if (identity != null)
{
// Get claim containing our setting
var _showInactiveRows = identity.FindFirst(ClaimTypes.UserData);
if (_showInactiveRows != null)
// Set the session value to the model
showInactive = bool.Parse(_showInactiveRows.Value);
}
return showInactive;
}
internal static async void setHideInactiveRows(ClaimsPrincipal user, bool hideInactiveRows)
{
var identity = (ClaimsIdentity)user.Identity;
if (identity != null)
{
// Get claim containing our setting
var _showInactiveRows = identity.FindFirst(ClaimTypes.UserData);
identity.RemoveClaim(_showInactiveRows);
identity.AddClaim(new Claim(ClaimTypes.UserData, hideInactiveRows.ToString(), ClaimValueTypes.Boolean));
/*if (SignInManager.IsSignedIn(User)) // Need to call this to refresh claims without logging out
{
var user = await UserManager.GetUserAsync(User);
await SignInManager.RefreshSignInAsync(user);
}*/
}
}
然后當我抓取資料時:
var hideInactiveRows = UserClaims.showInactiveRows(User);
if(hideInactiveRows)
{
FarmList = await _context.Farms
.Where(i => i.Active == hideInactiveRows) // Active = true show only active rows
.OrderBy(g => g.Name)
.ToListAsync();
} else {
FarmList = await _context.Farms
.OrderBy(g => g.Name)
.ToListAsync();
}
然而,這對我來說似乎也不合適。
有沒有更好的方法來按用戶更新和傳遞應用程式內部的資料?
uj5u.com熱心網友回復:
對于將來需要解決方案的任何人,我嘗試在 OnGet() 方法中檢查查詢引數,但這花費了太長時間,因為我必須修改 50 頁。
我想出的解決方案是簡單地創建一個單例服務:
public interface IUserMemStorage
{
// Using concurrent dictionary as it's thread safe
ConcurrentDictionary<int, string> UserHideInactiveRowStore { get; set; }
// Create other tmp storages here
}
// Note: Only register this interface as a singleton
// I created a new interface to explicitly mention singleton
public interface IUserMemStorageSingleton : IUserMemStorage { }
public class UserMemStorage : IUserMemStorageSingleton
{
public UserMemStorage()
{
UserHideInactiveRowStore = new();
}
public ConcurrentDictionary<int, string> UserHideInactiveRowStore { get; set; }
}
然后稍后在Startup.cs.
現在在我需要它的頁面上,我可以簡單地使用依賴注入容器并檢查是否應該基于userId. 無需修改任何反向鏈接或不同的導航頁面,因為它們bool存盤在執行緒安全字典中。:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/486769.html
上一篇:我的資料表沒有選擇json結果
下一篇:C#MVC集成測驗-System.InvalidOperationException:無法從根提供程式決議范圍服務“<APPNAME>.Data.ApplicationDbContext”
