无法访问我的Asp.Net Core项目中的帮助器类内的Session对象 [英] Cannot access Session object inside a helper class within my Asp.Net Core project
问题描述
我正在尝试访问ASP.NET Core 2.1项目内的帮助器类中的HttpContext.Session对象.
I am trying to access the HttpContext.Session object in a helper class within my ASP.NET Core 2.1 project.
当我尝试访问HttpContext.Session时,出现以下错误.
When I try to access HttpContext.Session I get the following error.
CS0120非静态字段,方法或属性'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"轻松访问它.
With .NET 4.x ASP.NET, it was easily accessed with "HttpContext.Current.Session".
这是我的课程:
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:
Here is my 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类中注入一些东西吗?
Do I need to inject something into the MySession class?
推荐答案
您仍然可以通过HttpContext
访问Session
.顾名思义,您将必须通过IHttpContextAccessor
访问会话,这将允许访问控制器和其他将其作为本地成员的框架类外部的HttpContext
.
You can still access Session
via the HttpContext
. You how ever have to access the session via the IHttpContextAccessor
, which as the name implies, will allow access to the HttpContext
external to controllers and other framework classes that have it as a local member.
首先,您需要将访问器添加到DI容器中.
First you need to add the accessor to the DI container.
services.AddHttpContextAccessor();
您需要从那里将其注入所需的类并访问所需的成员
from there you need to inject it into the desired class and access the desired members
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);
}
}
这篇关于无法访问我的Asp.Net Core项目中的帮助器类内的Session对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!