.net-core - 如何在 .net core 2.2 中使用此代码(HttpContext.Request.Current.Session[key])
问题描述
我无法在 .net core 2.2 版本 HttpContext.Current.Session["key"] 中使用此代码
HttpContext.Current.Session["key"]
HttpContext.Current.Session["key"]
解决方案
在 .NET Core 中不再有静态HttpContext.Current
,因为 .NET Core 有一个在 ASP.NET Core 内部使用的内置依赖注入系统。
如果您的代码在通常的 HTTP 请求范围内(中间件、控制器、视图等),则HttpContext
当前 HttpContext 有一个可用的属性。否则,您可以使用IHttpContextAccessor
(由 DI 注入)访问 HTTP 上下文。
您可以在此处找到所有详细信息:https ://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-2.2
如果您有 HttpContext 但 Session 为空,则必须启用 Session:https ://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.2#session -状态
推荐阅读
- javascript - 使用 Id 和 Bootstrap 样式解析表记录
- javascript - 使用 Vue Js 根据输入使文本可编辑/只读
- jquery - JQuery 提交按钮不起作用 Laravel
- r - 使用 highcharter 进行可视化 - R 中的 highcharter 错误
- java - 如何在不使用 BigInteger 数据类型的情况下在 Java 中添加两个非常大的数字,而不管它们的大小?
- java - Listview 不显示数据,但显示要显示值的空白位置
- c# - 在 RichTextBox WPF 中设置文本的垂直对齐问题
- c++ - 包括
在 GCC 4.8 中是必需的吗? - c# - 使用条件将嵌套的 for 循环转换为 linq
- java - 在 JSF 应用程序中保存来自 JMeter 的表单输入