jbyrd
jbyrd

Reputation: 5615

Session Variables in ASP.NET Core 1.0

I'm trying to do something simple in an ASP.NET Core 1.0 / ASP.net Core Mvc project: get and set simple session variables. But it has proven very difficult. I tried following the advice in this answer, which is to add the dependency -- "Microsoft.AspNet.Session": "1.0.0-beta3" -- to project.json, and then use Context.Session.SetInt("myVar", 35), but that did not work. The SetInt() method is still not recognized.

Do I need to include another dependency? What am I missing?

Upvotes: 0

Views: 1706

Answers (1)

Malgaur
Malgaur

Reputation: 1850

Try following this sample: https://github.com/aspnet/Session/blob/dev/samples/SessionSample/Startup.cs

You will need to configure the session middleware first, then include the appropriate usings.

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCaching();
        services.AddSession();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseSession(o => {
            o.IdleTimeout = TimeSpan.FromSeconds(30); });
    }
}

The relevant usings are

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;

Upvotes: 1

Related Questions