Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *** To use session, you need to configure session sevices ***
- builder.Services.AddDistributedMemoryCache();
- builder.Services.AddSession();
- // *** Also, you need to add session middleware - right before mapping of the controllers ***
- app.UseSession();
- // *** Action that uses session to store some data ***
- [HttpGet("[action]")]
- public ActionResult<Receipt> SessionData(string? data)
- {
- if (!string.IsNullOrEmpty(data))
- {
- HttpContext.Session.SetString("sessionData", data);
- return Ok($"Session data set to {data}.");
- }
- else
- {
- var storedData = HttpContext.Session.GetString("sessionData");
- return Ok($"Current session data is {storedData}.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement