gpt4 book ai didi

c# - 无法在我的 Asp.Net Core 项目中的帮助程序类中访问 Session 对象

转载 作者:行者123 更新时间:2023-11-30 19:52:58 32 4
gpt4 key购买 nike

我正在尝试访问我的 ASP.NET Core 2.1 项目中的帮助程序类中的 HttpContext.Session 对象。

当我尝试访问 HttpContext.Session 时出现以下错误。

CS0120 An object reference is required for the non-static field, method, or property 'HttpContext.Session'

在 .NET 4.x ASP.NET 中,可以通过“HttpContext.Current.Session”轻松访问它。

这是我的类(class):

 public class MySession
{

public void Foo()
{
HttpContext.Session.SetString("Name", "The Doctor"); // will not work
HttpContext.Session.SetInt32("Age", 773); // will not work

}
}

这是我的 Startup.cs:

    public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();

services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = System.TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});

services.Configure<ServiceSettings>(Configuration.GetSection("ServiceSettings"));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}

app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});

app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
}

我需要在 MySession 类中注入(inject)一些东西吗?

最佳答案

您仍然可以通过 HttpContext 访问 Session。您如何必须通过 IHttpContextAccessor 访问 session ,顾名思义,这将允许访问 Controller 和其他框架类外部的 HttpContext本地成员。

首先,您需要将访问器添加到 DI 容器。

services.AddHttpContextAccessor();

API Reference

从那里你需要将它注入(inject)所需的类并访问所需的成员

public class MySession {
IHttpContextAccessor accessor;

public MySession(IHttpContextAccessor accessor) {
this.accessor = accessor;
}

public void Foo() {
var httpContext = accessor.HttpContext;
httpContext.Session.SetString("Name", "The Doctor");
httpContext.Session.SetInt32("Age", 773);
}
}

关于c# - 无法在我的 Asp.Net Core 项目中的帮助程序类中访问 Session 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51939231/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com