In one of my Controller's actions I set TempData["key"] = true. When I attempt to read the value upon redirecting a user to another action it always returns null. I've read all the posts available here as well as Microsoft's docs, yet nothing seems to be helping to solve my issue. I haven't got an idea what else I could do.
In ConfigureServices:
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider();
services.AddSession();
In Configure():
app.UseSession();
app.UseMvc();
In the first action:
TempData["value"] = true;
In the second action:
var value = TempData["value"]; // --> this is always null
Could anyone say what I'm doing wrong here?
UPDATE 1
So now the ConfigureServices() looks like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ITempDataProvider, SessionStateTempDataProvider>();
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddSessionStateTempDataProvider();
services.AddSession();
var connectionString = "connectionString";
services.AddDbContext<PlannerContext>(o => o.UseSqlServer(connectionString));
services.AddScoped<IPlannerRepository>(provider =>
new PlannerRepository(provider.GetService<PlannerContext>()));
services.AddScoped<IScheduleSpecific>(provider =>
new ScheduleSpecific(provider.GetService<PlannerContext>()));
services.AddSingleton<ILoggerFactory, LoggerFactory>();
}
And the Configure() method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/error");
}
app.UseSession();
app.UseMvc();
}
I still can't get a value from TempData["key"]. It keeps returning null. I don't know what else to do. I'm beginning to feel like I'm banging my head over a brick wall...
UPDATE 2
I started a new project and used the same configuration settings as above. The result is the same as I described earlier: