user3106445
user3106445

Reputation: 351

Value of TempData becomes null after "Redirect"

I am facing issues with TempData after Redirect.

public ActionResult LoginCredentials()
{
    // Calling "SetError()" in catch(), if password mismatch.                        
    try{}

    catch()
    {
      return SetError();
    }   
}

public ActionResult SetError()
{
    // set the value of TempData as "true"                        
    TempData["error"] = true;
    return Redirect("/Login");                
}


public ActionResult Index()
{
    ViewData["useError"]= TempData["error"]; // at this point TempData["error"] is null.
    ...
}

In SetError() value of TempData is successfully set as true, issue takes place after "Redirect", value becomes "null" and I can't use it anymore.

Upvotes: 8

Views: 16841

Answers (6)

Cathal
Cathal

Reputation: 11

Setting the cookie as essential in CookieTempDataProviderOptions Solved the issue for me.

services.Configure<CookiePolicyOptions>(options =>
{
    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    options.CheckConsentNeeded = _ => true;
    options.MinimumSameSitePolicy = SameSiteMode.Strict;
});
services.Configure<CookieTempDataProviderOptions>(options =>
{
    options.Cookie.IsEssential = true;
});

More info: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.cookietempdataprovideroptions.cookie?view=aspnetcore-7.0

Upvotes: 1

user12282614
user12282614

Reputation: 49

I find .Net Core incredibly problematic. I had to turn this off in the Configuration

options.CheckConsentNeeded = context => true;

and it worked when I used Redirect to navigate to another page.

However on refreshing the page the TempDate or ViewData lose their value. But when I reassigned it to itself in the "View" it worked:

@{
TempData["somevalue"] = TempData["somevalue"];

}

Upvotes: 2

Aziz Nortozhiev
Aziz Nortozhiev

Reputation: 435

    services.Configure<CookiePolicyOptions>(options =>
    {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    //options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
    });

turn off options.CheckConsentNeeded = context => true; 

this worked for me

Upvotes: 7

aspark
aspark

Reputation: 370

  1. maybe the browser is cookieless
  2. the data in a TempDataDictionary object persists only from one request to the next, unless you mark one or more keys for retention by using the Keep method, accoding to your code, if you redirect to login page, and then redirect to index, the value will be null. you can only read it at login page.

Upvotes: 5

Vivek Ranjan
Vivek Ranjan

Reputation: 1492

Use RedirectToAction

public ActionResult SetError()
{
// set the value of TempData as "true"                        
   TempData["error"] = true;
   return RedirectToAction("YourViewName");                
 }

Upvotes: 1

Disappointed
Disappointed

Reputation: 1120

As far as i understand ViewData saves data only after redirect, not when just another Http request occur. So inside Login method(where you redirect to) this ViewData["useError"] have to be available, but Index method is just another method which is executed during another http request.That's why ViewData["useError"] is empty
You can use Session if you want to store data between different Http requests

Upvotes: 0

Related Questions