0

In one of my Controller's actions I set TempData["key"] = true. When I attempt to read the value upon redirecting a user to another action it always returns null. I've read all the posts available here as well as Microsoft's docs, yet nothing seems to be helping to solve my issue. I haven't got an idea what else I could do.

In ConfigureServices:

services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDistributedMemoryCache();

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

        services.AddSession();

In Configure():

app.UseSession();
app.UseMvc();  

In the first action:

TempData["value"] = true;

In the second action:

var value = TempData["value"]; // --> this is always null

Could anyone say what I'm doing wrong here?

UPDATE 1

So now the ConfigureServices() looks like this:

public void ConfigureServices(IServiceCollection services)
{

    services.AddSingleton<ITempDataProvider, SessionStateTempDataProvider>();

    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => false;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDistributedMemoryCache();

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

    services.AddSession();

    var connectionString = "connectionString";

    services.AddDbContext<PlannerContext>(o => o.UseSqlServer(connectionString));

    services.AddScoped<IPlannerRepository>(provider =>
        new PlannerRepository(provider.GetService<PlannerContext>()));

    services.AddScoped<IScheduleSpecific>(provider =>
        new ScheduleSpecific(provider.GetService<PlannerContext>()));

    services.AddSingleton<ILoggerFactory, LoggerFactory>();
}

And the Configure() method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/error");
    }



    app.UseSession();
    app.UseMvc();



}

I still can't get a value from TempData["key"]. It keeps returning null. I don't know what else to do. I'm beginning to feel like I'm banging my head over a brick wall...

UPDATE 2

I started a new project and used the same configuration settings as above. The result is the same as I described earlier:

null TempData value

Configuration

Dependencies

9
  • I read the post you quote several times. None of the things mentioned there helped me and that is why I decided to ask a separate questions. Plus the post you're referring to seems to be about earlier version of ASpNetCore. Commented Dec 9, 2018 at 18:07
  • Well, the answers over there don't make much sense. I have reopened. Commented Dec 9, 2018 at 18:54
  • And FWIW, I cannot rerproduce this. I tried in a fairly small 2.1 app and the value persists, both with and without AddSession(). Commented Dec 9, 2018 at 18:56
  • @HenkHolterman Thank you. What does "FWIW" mean? Commented Dec 9, 2018 at 19:21
  • 2
    After Update 2: Only now can we see that it is about an API. So cookies won't work for sure, and SessionState depends on cookies (SessionId). So I would guess TempData simply doesn't work for an API. Commented Dec 9, 2018 at 20:32

1 Answer 1

1

There are two different built-in providers for temp data: SessionStateTempDataProvider and CookieTempDataProvider. The latter will directly use a cookie to persist the temp data for the user. The former will use the session state to persist the temp data. Since the session state itself also uses a cookie to identify the user session, both solutions are dependent on cookies.

Now, in a typical API situation, you are dealing with REST which is stateless by definition. That means that cookies, a mechanism to keep state, are not used with it. So you can use neither temp data nor session state within your API.

While it would be possible to set up your own temp data provider that uses e.g. a user id from your authentication to look up temp data from a central store, doing so would again introduce state into your API which is generally discouraged.

Instead, you should make sure that your API is as stateless as possible and that the client passes everything necessary with the request.

Of course, if you don’t want to do REST, you could also just require the API clients to locally store cookies.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.