0
I’m trying to set up a session, but it always gets null, look:
public class AutenticacaoModel : PageModelBase
{
public void OnGet()
{
HttpContext.Session.SetString("Teste", "Quero ver esse valor");
}
{
public class IndexModel : PageModelBase
{
public void OnGet()
{
Response.WriteAsync(HttpContext.Session.GetString("Teste"));
}
}
The value always comes null, and gives error.
Check out my Startup:
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 => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMemoryCache();
services.AddSession(options =>
{
options.Cookie = new Microsoft.AspNetCore.Http.CookieBuilder()
{
Name = "SINUCABRASIL.SESSION",
Expiration = TimeSpan.FromMinutes(30)
};
});
services
.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions
.AddPageRoute("/Autenticacao", "usuario/autenticacao")
.AddPageRoute("/MeuPerfil", "usuario/perfil")
.AddPageRoute("/MeusLocais", "usuario/estabelecimentos")
.AddPageRoute("/MeusTorneios", "usuario/eventos")
.AddPageRoute("/MinhasInscricoes", "usuario/inscricoes")
.AddPageRoute("/PoliticaPrivacidade", "privacidade/politica-de-privacidade")
.AddPageRoute("/TermosUso", "site/termos-de-uso")
.AddPageRoute("/Sobre", "site/sobre")
.AddPageRoute("/OndeJogar", "estabelecimentos")
.AddPageRoute("/Evento", "eventos")
.AddPageRoute("/Ranking", "rankings")
.AddPageRoute("/Jogador", "jogadores")
.AddPageRoute("/Contato", "suporte/fale-conosco")
.AddPageRoute("/Faq", "suporte/faq")
.AddPageRoute("/Configuracoes", "configuracoes");
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseSession();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseMvc();
}
Apparently your code is right, you went through
OnGet()
ofAutenticacaoModel
before entering the other page?– Barbetta
Opa, yes, that’s what I call: Httpcontext.session.Setstring("Test", "I want to see this value");
– Eduardo Vieira
I recreated the project, and then it worked. In another project, I have the same problem.
– Eduardo Vieira
Probably some reference problem, you tried on a different computer?
– Barbetta
I recreated the project, and then it worked. Pretty messed up.
– Eduardo Vieira