21434

Context keyword doesn't get recognized in ASP.net 5 MVC Controller for use with Sessions

Question:

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

<blockquote>

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

</blockquote>

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. <a href="https://stackoverflow.com/questions/29420262/sessions-in-asp-net-5" rel="nofollow">Link1</a>

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

Answer1:

Made some renames on beta 8, here is a helpful <a href="https://christopherhackett.co.uk/blog/2015/10/17/ASP.NET-5-MVC-6-renames-in-beta-8/" rel="nofollow">link</a>

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()

Answer2:

Solution is to use HttpContext

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

Recommend

  • C# extension method for a method group
  • Cookie Authentication Early Expiration
  • How do I configure routing for MVC and SignalR in an ASP.NET Core project?
  • ASP.NET core - middleware on MVC
  • asp.net routing syntax with no action
  • Routing by slug in asp.net mvc
  • ASP.NET Core 2.0 AzureAD Authentication not working
  • Url.Action(action,controller,routeValues) doubling up ID in URL
  • Elmah not logging 404 (missing files / images)
  • Constant embedded for loop condition optimization in C++ with gcc
  • Spark dataframe to nested map
  • why route of backbone needs the default route
  • Manage different base layouts in Angular2
  • Is MVC2 ASP.Net URLDecoding automatically?
  • ASP.NET MVC - Detect Time Spent on Page
  • QueryString id parameter not being used
  • React Router and Arbitrary Query Params: Page Refreshes Unintentionally on Load?
  • What is the likely cause of a net::ERR_CONNECTION_ABORTED when uploading a file to Spring
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • PWA with multiple pages
  • Python to parent/child JSON
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Microsoft Excel Pivot miscalculation in Sum for positive and negative numbers
  • Unable to click on the next page button containing “>” sign
  • Is there a way to set up a fallback for the formAction attribute in HTML5?
  • Can I use AllJoyn Framework for Wifi Direct in iOS?
  • Enabling DTD support in Sql Server
  • Installing Apache MyFaces 2 on WildFly 8.2.0
  • Hardware Accelerated Image Scaling in windows using C++
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Can I make an Android app that runs a web view in Chrome 39?
  • Symfony2: How to get request parameter
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How to include full .NET prerequisite for Wix Burn installer
  • log4net write single file for each call to log.info
  • Getting error when using KSoap library to consume .NET web services
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?