asp.net-core - ASP NET Core MVC - 如何配置进程外 session 状态?

标签 asp.net-core asp.net-core-mvc

有没有办法使用 ASP.NET Core MVC 配置进程外 session 状态(使用 Windows 状态服务器或 SQL Server)?

最佳答案

重要的是 session 数据由缓存支持。您需要将 IDistributedCache 实现添加到您的应用程序服务,而不是内存缓存,即:

对于 SQL Server:

services.AddDistributedSqlServerCache(options =>
    {
        options.ConnectionString = @"your_connection_string";
        options.SchemaName = "dbo";
        options.TableName = "TestCache";
    });

(确保您的项目中添加了 Microsoft.Extensions.Caching.SqlServer 包)

对于 Redis:

services.AddDistributedRedisCache(options =>
    {
        options.Configuration = "localhost";
        options.InstanceName = "SampleInstance";
    });

(包:Microsoft.Extensions.Caching.Redis)

我不知道是否有针对 ASP.NET Core 分布式缓存的 Windows State Server 的实现。

分布式 session 的文档链接:https://learn.microsoft.com/en-us/aspnet/core/performance/caching/distributed

有关 ASP.NET Core 中 session 的更多信息:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state

关于asp.net-core - ASP NET Core MVC - 如何配置进程外 session 状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536012/

相关文章:

c# - asp.core 页面 Razor 枚举下拉列表

c# - 使用特定的 HttpMessageHandler 注入(inject)单实例 HttpClient

c# - 将依赖项注入(inject) .net 核心中的自动映射器配置文件 - IHttpContextAccessor 返回 null

asp.net-core - Asp.net core 模型绑定(bind)在发布请求后不显示值

c# - 基于ASP.NET Core中的Controller的条件要求

asp.net-core - UseStatusCodePagesWithRedirects 和 UseStatusCodePagesWithReExecute 之间的区别 - Asp.net core 中的状态代码页

asp.net - 为什么从 tsconfig 中排除 wwwroot?

c# - 运行 dotnet publish -c Release -o out 时,在 ASP.NET Core 项目中构建 Dockerfile 失败

azure - DotNet Core Web 应用程序在本地工作,但在部署到 Azure 时无法工作

c# - Blazor 将输入值绑定(bind)到 oninput 不适用于 onkeypress