Session has not been configured for this application or request error
Solution 1:
In your Startup.cs you might need to call
app.UseSession before app.UseMvc
app.UseSession();
app.UseMvc();
For this to work, you will also need to make sure the Microsoft.AspNetCore.Session nuget package is installed.
Update
You dont not need to use app.UseMvc(); in .NET Core 3.0 or higher
Solution 2:
Following code worked out for me:
Configure Services :
public void ConfigureServices(IServiceCollection services)
{
//In-Memory
services.AddDistributedMemoryCache();
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromMinutes(1);
});
// Add framework services.
services.AddMvc();
}
Configure the HTTP Request Pipeline:
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Solution 3:
I was getting the exact same error in my .NET Core 3.0 Razor Pages application. Since, I'm not using app.UseMvc()
the proposed answer could not work for me.
So, for anyone landing here having the same problem in .NET Core 3.0, here's what I did inside Configure
to get it to work:
app.UseSession(); // use this before .UseEndpoints
app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });
Solution 4:
For .NET 6:
Go to your Program.cs
first (to add the code there)
I inserted this at the last part of the builder:
builder.Services.AddSession();
I added this after app.UseAuthorization();
app.UseSession();
Final Output:
Note: I don't know if this is really the "right lines" to place the code, but this is what worked for me. (I'm still a beginner in ASP.NET as well)