TempData isn't persisting data on Production Server [ASP.NET CORE 2.1]

843 Views Asked by At

I have an issue and I have searched the docs to see maybe I made a mistake or something but didn't find the answer that I was looking for. My issue is this, After saving a product, I use the PRG(Post-Redirect-Get) pattern so the user doesn't resubmit the form when pressing the browser back button. I display a one time message using TempData since it persists data to the next request before deleting. This method works fine on my development environment. But after I deploy the application to a production server, although the action methods follow the PRG pattern and return successfully, the TempData isn't persisted and the message isn't shown.

Here is how I display the message:

@if (TempData["Message"] != null)
{
    var message = JsonConvert.DeserializeObject<MessageModel>(TempData["Message"].ToString());
<div class="alert [email protected] text-center alert-dismissible fade show fixed-bottom w-100" role="alert" style="margin:0">
    <h6 class="text-white">@message.Message</h6>
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
</div>
}

Here is the method sample:

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(Product model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        var product = await _productRepository.GetProductAsync(model.ID);


        product.Name = model.Name; product.ProductCode = model.ProductCode; product.Category = model.Category;
        product.Description = model.Description;
        await _productRepository.UpdateAsync(product);

        TempData["Message"] = JsonConvert.SerializeObject(new MessageModel { Class = "success", Message = "Product has been successfully updated" });
        return RedirectToAction("Edit", new { id = model.ID });
    }

Any help would be appreciated. Thanks!

1

There are 1 best solutions below

1
On

Well, due to General Data Protection Regulation support in Asp.net Core, if the user(in this case me) hasn't agreed to data collection and the app has CheckConsentNeeded set to true, all non-essential cookies(TempData, Session cookies) are not sent to the browser. TempData and Session cookies need tracking to be enabled before they become functional. So the solution is either implement the cookieConsentPartial file and agree by clicking ok or set checkConsentNeeded to its default which is false. You can read more here EU General Data Protection Regulation (GDPR) support in ASP.NET Core