问题
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.
回答1:
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()
回答2:
Solution is to use HttpContext
HttpContext.Session.SetString("MyString", "test");
来源:https://stackoverflow.com/questions/33210026/context-keyword-doesnt-get-recognized-in-asp-net-5-mvc-controller-for-use-with