'.Net 6 - Different session for each browser tab – diff images in each
I been having some major issues with this, I feel like this shouldn’t be so hard but.. I am trying to be able to open a link which brings pictures to the browser to display them. I have this working. What I am trying to do is when someone opens another link itll open a new tab and they will have those new pictures in that tab and the previous pictures in the other tab. I tried to do this with Sessions but it overwrites the session, and both tabs show the same pics. I now coded it up so I have a different session guid for each tab and I go to try to view the pics but the same thing happens. The tab is over written. My new idea is to save this huge text file in a hidden variable on the page and read from that. But when I do this my tempdata var says the page cannot be found but if I remove the tempdata var the page works… I rewrote a bunch of methods some multiple times. Any ways to make this work would be greatly appreciated
public async Task<ActionResult> PopulateFileCache(string folderPath)
{
if (!string.IsNullOrEmpty(folderPath))
{
using var client = _httpClient.CreateClient("MesserAPI");
try
{
using var response = await client.GetAsync($"/api/File/GetFolderFiles?folderPath={folderPath}");
if (response.IsSuccessStatusCode)
{
var lstFiles = await response.Content.ReadAsStringAsync();
ViewData["lstParts"] = lstFiles;
TempData["SlideTotal"] = 0;
if (_contextAccessor.HttpContext.Session.IsAvailable)
{
SaveToSession(lstFiles);
}
TempData["guid"] = _contextAccessor.HttpContext.Session.GetString("sGUID") ?? string.Empty;
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
}
return RedirectToAction("FileViewer");
}
public IActionResult ReturnData(string sessionID)
{
//if (objFiles != null)
//{
if (TempData != null)
{
TempData.Keep();
}
List<LNPartVM> lNParts = null;
try
{
//var jsonResponse = objFiles;
//var jsonResponse = GetFromSession();
var jsonResponse = GetFromSession(sessionID);
if (!string.IsNullOrWhiteSpace(jsonResponse))
{
lNParts = JsonSerializer.Deserialize<List<LNPartVM>>(jsonResponse);
}
if (lNParts?.Count > 0)
{
TempData["SlideTotal"] = lNParts.Count;
_contextAccessor.HttpContext.Session.SetInt32("SlideTotal", lNParts.Count);
int partIndex = 0;
try
{
partIndex = (int)(_contextAccessor.HttpContext.Session.GetInt32("PartIndex") == null ? 0 : _contextAccessor.HttpContext.Session.GetInt32("PartIndex"));
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
LNPartVM partVM = lNParts[partIndex];
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
//}
return ViewComponent("DataPage");
}
public string GetSessionID()
{
return _contextAccessor.HttpContext.Session.Id;
}
public string GetSessionGUID()
{
return Guid.NewGuid().ToString();
}
//string sessionName,
public IActionResult SaveToSession(string obj)
{
bool status = false;
// Same Session across browsers
//string sessionID = GetSessionID();
string sessionID = GetSessionGUID();
if (!string.IsNullOrWhiteSpace(sessionID))
{
_contextAccessor.HttpContext.Session.SetString(sessionID, obj);
_contextAccessor.HttpContext.Session.SetString("sGUID", sessionID);
status = true;
}
else
{
_contextAccessor.HttpContext.Session.SetString("SessionPN", obj);
status |= true;
}
if (status)
{
return Content("Session Saved");
}
return Content("Session Error");
}
public async Task<IActionResult> FileViewer()
{
return View();
}
This is on the HTML page
$('#btnNext').click(function (e) {
e.preventDefault();
console.log(iSlideCount);
if (iSlideCount > 0) {
$.ajax({
type: 'GET',
url: '@Url.Action("NextPart", "File")',
contentType: 'Json'
})
.done(function(e) {
var sguid = $("#txthguid").val();
var route = '@Url.Action("ReturnData","File", new { sessionID="-1" })';
route = route.replace("-1", sguid);
$('#contentData').load(route);
//$('#contentData').load('@Url.Action("ReturnData","File")');
//$('#part').load('@Url.Action("ReturnData","File")');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
