I am using Visual Studio 2022
To Set the session in Asp.Net Core 7.0 MVC project, do the following changes :- ___________________________________________________________________________________
In Program.cs file:-
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddSession();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=HR}/{action=HRLogin}/{id?}");
app.Run();
____________________________________________
In Controller:-
HttpContext.Session.SetString("username", login.Username.ToString());
then call the session value
string SessionName = HttpContext.Session.GetString("username");
No comments:
Post a Comment