asp.net - .net core 3.1 如何添加HttpContext.Current和HttpContext.Current.Application[""]
问题描述
我在这里阅读了一些帖子,似乎 System.Web 和 HttpContext.Current 已从 .net 核心中删除。我在我的 startup.cs 中添加以下内容
services.addSingleton<IHttpContextAccessor, HttpContextAccesor>();
在将 HttpContext.Current 添加到我的班级时不起作用
在我的类库中,我将 Microsoft.AspNetCore.http 安装到我的依赖项中。我添加为 DI
public MyClass(IHttpContextAccessor _httpContextAccessor) {
httpContextAccessor = _httpContextAccessor;
}
在我的cs代码中,旧代码以
if(HttpContext.Current != null && HttpContext.Current.Application["Test"] != null])
什么是替代品,到目前为止这不起作用
httpContextAccessor.HttpContext.Session != null
Not sure what is the equivalent to HttpContext.Current.Application["Test"]
谢谢
解决方案
首先,在 Startup.cs 中注册服务,
services.AddHttpContextAccessor();
然后你可以在任何类中调用下面的代码,它取代了 HttpContext.Current
private HttpContext _httpContext => new HttpContextAccessor().HttpContext;
您可以使用_httpContext.Items[]
代替应用程序。(生产前测试)
推荐阅读
- javascript - 在 Node.js 中使用 Azure SendGrid 发送电子邮件
- timeout - 如何更改 Frameworkium 中的等待超时 - 全局
- android - 包 android.support.v4.view 不存在,即使添加了依赖项
- php - 使用 Ajax 从公共目录下载 **laravel 5.4 ** 中的 Zip 文件
- mysql - mysql 5.5 命令行客户端错误 1366 HY000:不正确的字符串值:'\xD3\xE3\xC0
- python - sympy.utilities.iterables.combinations() 替换?
- java - 如何在 Spring 中配置 @Valid 上的直接字段访问?
- kdb - Q 中的大整数类型
- azure-active-directory - 通过 Azure 刀片或在 Azure AD B2C 中以编程方式编辑自定义属性的值
- kubernetes - Kubernetes 上的 Istio:pod 到服务的通信不起作用