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?

7
  • is there a particular reason you need session variables? Personally it was the first thing I used to turn off on the old templates, only turning them on when I had exhausted all other possible ways of not using them as sessions variables are a scalability and performance bottleneck. Commented Apr 6, 2015 at 14:09
  • I'm conditionally setting the Layout variable in _ViewStart.cshtml to allow our developers to set a "?dev=1" query string to develop in a new layout that we don't want to propagate globally yet. Right now that works and they can go to "some/page?dev=1" to see the page they're developing on in the new template. I want this choice to hold through the session, so they can navigate through the site and keep that new layout. Commented Apr 6, 2015 at 14:14
  • 1
    Have you added using Microsoft.AspNet.Http; to your controller file? Commented Apr 7, 2015 at 12:20
  • just tried myself and after adding the dependency on project json and the using as @MikeBrind suggested I have the session working ok. Commented Apr 7, 2015 at 18:38
  • 1
    I've blogged about how to use Session in ASP.NET5 if it helps: mikesdotnetting.com/article/270/sessions-in-asp-net-5 Commented Apr 7, 2015 at 20:23

1 Answer 1

1

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;
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.