Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TempData is always Null at ASP.Net Core 2.1 MVC

I would like to use TempData in my .net core mvc application. I followed the article from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata

I always get NULL Here is my code:

public async Task<ActionResult> Index(RentalsFilter filter)
{
    TempData["test"] = "ABC";
    return View();
}

public ActionResult Create()
{
    var abc = TempData["test"].ToString();
    return View();
}
like image 551
Si Thu Avatar asked Jul 23 '18 16:07

Si Thu


People also ask

Can we use TempData in .NET core?

TempData in MVC is used to pass data from Controller to Controller or Controller to view and it is used to pass data that persists only from one request to the next. TempData requires Typecasting. And also check for Null value before reading the value from it.

What is TempData in ASP.NET MVC?

ASP.NET MVC - TempData TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value.


3 Answers

Had similar issue due to GDRP (https://docs.microsoft.com/en-us/aspnet/core/security/gdpr?view=aspnetcore-2.1). If you want have it up and running without worring about GDPR you can just disable it. The config below also uses cookies (default) instead of session state for TempData.

Startup.cs

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

        services.Configure<CookieTempDataProviderOptions>(options =>
        {
            options.Cookie.IsEssential = true;
        });

...

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy(); // <- this

        app.UseAuthentication();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
like image 63
Artur Kędzior Avatar answered Oct 06 '22 01:10

Artur Kędzior


Did you configure TempData as said in the doc:

in ConfigureServices method add:

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddSessionStateTempDataProvider();

services.AddSession();

And in Configure method you should add:

app.UseSession();
like image 30
othman.Da Avatar answered Oct 06 '22 02:10

othman.Da


The answer that worked for me (for asp.net Core 2.2) was

in Startup.Configure() app.UseCookiePolicy(); should be after app.UseMVC();

Which someone above has linked to in the comments from this stackoverflow answer

This is in addition to having

app.UseSession() (in Configure)

and

services.AddSession() (in ConfigureServices)

like image 41
jazza1000 Avatar answered Oct 06 '22 03:10

jazza1000