你好朋友,我正在使用asp net core,我想将数据保存在我的会话变量中,我已经尝试了一段时间,但是它标志着会话中的错误
当前上下文中不存在名称“会话”
这是我的方法。
public IActionResult LoginPost(LoginInfo paramsForm)
{
var mailx = paramsForm.email;
var passx = paramsForm.pass;
//Session["email"] = mailx;
// HttpContext.Session.SetString("email_user", mailx);
Session["Email"] = mailx; // <- aquí aparece
return View("Test");
}
启动
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
// 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("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
如果我们分析文档
ASP.NET Core 中的应用程序和会话状态
在标题中,
Configurar el estado de sesión
您将看到您必须启用session
要检索您的值,
Session
必须使用中指定的扩展方法ISession
。见这里。例子
您可以在以下情况下使用session属性:
在您的情况下,代码应如下所示: