How to use sessions in ASP vNext

How to use session variables in ASP MVC 6?

I could not find a working sample of how to store and use session variables. Can anyone help?

+6
source share
2 answers

add the package "Microsoft.AspNet.Session": "1.0.0-beta8" to project.json, and then using Microsoft.AspNet.Http;

inside this namespace you have extension methods for the context.

you also need to use it with DI on Startup.cs :

 public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddSession(); } 

Here's an example of a controller:

 using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; namespace MvcWebApp.Controllers { [Route("[controller]")] public class SomeController : Controller { public async Task<IActionResult> Edit() { HttpContext.Session.SetInt("myVar", 35); } } } 
+4
source

there is a sample on the github repo session: https://github.com/aspnet/Session/tree/release

And you can access the session using the Controler Session property

+1
source

Source: https://habr.com/ru/post/984388/


All Articles