正在努力寻找一个合适的解决方案来跨多个页面访问 int userId 值
所以我不是专家。正当你觉得自己有能力的时候,这样的问题就会出现。
我有一个使用 Identity 的 asp.net core razor Web 应用程序项目(使用 VS 2022)。 Identity 正在工作,但我真的想要一个 int userId 而不是字符串,但完全放弃尝试修改 Identity 以使用 int 作为 userId 而不是默认字符串。
因此,我实现了一个数据库表,其中包含所有自定义用户属性,其中包含身份字符串 userId 和 int 键。这有效。
所以有多个页面我想获取int userId。
在 razor 页面中,我可以使用依赖项注入来注入 UserManager userManager。我可以在需要 int userId 的每个 razor 页面上执行此操作,并使用 GetUserId,然后使用带有字符串 userId 的查询来查找 int userId。然而,这意味着在我需要 userId 的每个 razor 页面上复制代码,因此这远不是一个好的解决方案。我想我应该尝试实现一个静态类来做到这一点,但这似乎无法访问 httpContext.Session 所以这不起作用(除非我错过了一些东西)。
让事情变得复杂的是,我认为我可以将 int userId 存储在 Session 中,但这将会过期,因此每当我读取 Session 变量时,我需要检查它是否为空并且是否为有效值,如果它没有刷新会话通过获取字符串 userId,找到 int userId 并将其保存到会话变量中。再次将该代码放在每个页面上并不好。
但是,如果我创建一个类来执行此操作,它没有 dbContext,并且我再次遇到这样的问题:如果它是静态类,则它似乎无法访问 Session。
这感觉应该是一件很简单的事情,但我已经让自己陷入了“我到底要做什么”的情况之一。我尝试了很多谷歌搜索,但似乎没有解决问题
所以有多个页面我想获取int userId。
所有 Razor 页面都继承自
PageModel
类。您可以将“获取 int userid”的 Razor 页面设置为从自定义页面模型继承,如本SO 帖子中所建议的。这允许将集中/共享代码添加到继承的类中。继承的类提供对:DI服务、HttpContext
、User
等的访问。
从基本页面模型继承的 Razor 页面可以访问自定义基本页面模型中定义的属性。
以下示例展示了如何通过依赖项注入设置继承的基页模型。该示例使用
IConfiguration
DI 服务替代需要访问数据库服务的场景。
属性
UserIdAsInt
是在继承的基本页面模型中定义的。请注意,该属性是在 Razor 视图页面的 OnGet()
方法中设置的,因为继承类中的 GetUserIdAsInt()
使用了 HttpContext
对象。该对象在继承类或 Razor 代码隐藏类的构造函数方法中不可用。一旦代码执行到 HttpContext
方法,OnGet()
就可用。
MyBasePageModel.cs
这个 C# 类文件可以插入项目中的任何位置。如果命名空间不是
using
,您只需要在代码隐藏中使用 .cshtml.cs
语句 [ProjectName].Pages
。
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApplication1.Pages
{
public class MyBasePageModel : PageModel // Inherits from 'PageModel'
{
private readonly IConfiguration _configuration;
public int UserIdAsInt { get; set; }
public MyBasePageModel(IConfiguration configuration)
{
_configuration = configuration;
}
public int GetUserIdAsInt()
{
int? id = HttpContext.Session.GetInt32("UserIdAsInt");
if (id == null)
{
// Get user ID as integer from database
id = 100; // For sample purpose set 'id' variable to a hardcoded value
HttpContext.Session.SetInt32("UserIdAsInt", (int)id);
System.Diagnostics.Debug.WriteLine("User ID retrieved from database");
}
else
{
System.Diagnostics.Debug.WriteLine("User ID retrieved from session");
}
return (int)id;
}
public string GetLoggingLevel()
{
return _configuration["Logging:LogLevel:Default"].ToString();
}
}
}
继承自BasePageModel.cshtml.cs
namespace WebApplication1.Pages
{
public class InheritFromBasePageModelModel : MyBasePageModel // Inherits from the custom 'MyBasePageModel' class
{
public string? LoggingLevel { get; set; }
public InheritFromBasePageModelModel(IConfiguration configuration)
: base(configuration: configuration)
{
}
public void OnGet()
{
UserIdAsInt = GetUserIdAsInt();
LoggingLevel = GetLoggingLevel();
}
}
}
继承自BasePageModel.cshtml
@page
@model WebApplication1.Pages.InheritFromBasePageModelModel
@{
}
@section Styles {
<style>
p > span {
font-weight: bold;
}
</style>
}
<p>Logging level property from base page model: <span>@Model.LoggingLevel</span></p>
<p>Integer value: <span>@Model.UserIdAsInt</span></p>
程序.cs
// https://stackoverflow.com/questions/71070698/session-in-asp-net-core-mvc
// "add session middleware in your configuration file"
builder.Services.AddSession(options =>
{
//options.IdleTimeout = TimeSpan.FromMinutes(1);
});
app.UseSession();