0

I am using ASP.net 5 Beta 8. I want to make use of Sessions however Context keyword is not being understood.

In my packages.json

"Microsoft.AspNet.Http": "1.0.0-beta8",
"Microsoft.AspNet.Session": "1.0.0-beta8",

In my ConfigureServices of Startup.cs

 // Add MVC services to the services container.
 services.AddMvc();

 //Session Support
 services.AddSession();
 services.AddCaching();

In Configure of Startup.cs

        //Session
        app.UseSession();

        // Add MVC to the request pipeline.
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
      });

In HomeController.cs

using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Session;
using Microsoft.AspNet.Http;


public IActionResult Index()
{
    Context.Session.SetString("MyString", "test");
    return View();
}

And the error I get is

The name 'Context' does not exist in the current context

I also tried removing dnx core from project.json

"frameworks": {
   "dnx451": { }
 },

but it also doesn't work.

Take note: I have used the previous answers on stackoverflow to try resolve the issue and it hasn't worked. e.g. Link1

I also tried various blog posts on Sessions in ASP.NET 5 but I still get the same error.

2 Answers 2

2

Made some renames on beta 8, here is a helpful link

Older                               Beta 8
Context                             HttpContext
Context.Session.Set(String, byte[]) HttpContext.Session.SetInt32(String, byte[])

Diagnostics Error pages provided by diagnostics have better names to avoid confusion.

app.UseDeveloperExceptionPage();

Older                  Beta 8
app.ErrorHandler()     app.UseExceptionHandler()
app.ErrorPage()        app.UseDeveloperExceptionPage()
Sign up to request clarification or add additional context in comments.

1 Comment

Please do not just post links as an answer, as they might get deleted.
1

Solution is to use HttpContext

 HttpContext.Session.SetString("MyString", "test");

1 Comment

ASP.NET 5 BETA 8 has renamed the Context to HTTPContext in the controller so it was just a simple rename

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.