Minifycode 2021-10-03 Viewed 1.5K times ASP.Net Core MVC

In this article, you will learn what is Session State Management
In ASP.NET Core

In this article, The app uses the session ID to fetch the session data. ASP.NET Core maintains

the session state by providing a cookie to the client that contains a session ID, which is sent

to the app with each request. 

Configuring session

Microsoft.AspNetCore.Session package provides middleware to manage the sessions in

ASP.NET Core. To use the session in our Application, we need to add this package as

a dependency in the project.json file.

 

Startup.cs

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DBCON":"server=.;database=dbname; trusted_connection=true" 
  }
}
public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. 
        //Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped(sp => sp.GetRequiredService<IHttpContextAccessor>().HttpContext);
            services.AddSession(options =>
            {

            });
            
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
     

public void Configure(IApplicationBuilder app, IWebHostEnvironment 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.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }

 


using Microsoft.AspNetCore.Http;  
using Microsoft.AspNetCore.Mvc;  
  

public class HomeController : Controller  
{  
  
    [Route("home/index")]  
    public IActionResult Index()  
    {  
        HttpContext.Session.se.SetString("email","info@minifycode.in");
        return View();  
    }  
    [Route("home/GetSessionData")]  
    public IActionResult GetSessionData()  
    {  
        ViewBag.data = HttpContext.Session.GetString("email");
        return View();  
    }  
}  

SetString session

HttpContext.Session.SetString("email", "1234");

GetString session

string email = HttpContext.Session.GetString("email");

Check session

 if (HttpContext.Session.GetString("email") == null)
            {
                    // your code here 
             }

Session State Management In ASP.NET Core, The app uses the session ID to fetch the session data. ASP.NET Core maintains the session state by providing a cookie to the client that contains a session ID, which is sent to the app with each request.
minify code