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

限于喜欢 提交于 2019-12-13 07:40:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!