我正在尝试在我的ASP.NET Core 1.0(vNEXT)项目中使用Session.这就是我的代码的样子.
I am trying to use Session in my ASP.NET Core 1.0 (vNEXT) project. This is how my code looks like.
public class Startup { // For more information on how to configure your application, visit go.microsoft/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { //-- Enabled MVC Pattern services.AddMvc(); //-- Enable Session handling services.AddCachingServices(); services.AddSessionServices(); } public void Configure(IApplicationBuilder app) { //-- User MVC app.UseMvc(); //-- Use Session app.UseSession(); app.UseInMemorySession(); } }但是现在我被困住了.我不知道如何在控制器或页面中的会话中设置或获取数据.我已经尝试过this.Context.Session.但是它没有被实例化,所以当我尝试访问该属性时会抛出一个invalidexception.
But now I am stuck. I don't know how to set or get data from the session in my controller or pages. I have tried the this.Context.Session. But it's not instanced, so an invalidexception is throw when i try to access that properties.
任何具有有关如何使其正常工作的代码示例的人.
Anybody that has an code example on how to get this working.
谢谢.
以下是屏幕转储,其中base.context.Session在我的控制器中是什么样的.
Here is a screendump how the base.context.Session looks like in my controller.
我没有SetString()和GetString(),只有Set()和TryGet()...方法...
And I don't have the SetString() and GetString(), only the Set() and TryGet()... methods...
这是我的project.json文件的样子.
This is how my project.json file looks like.
"version": "1.0.0-*", "dependencies": { "Microsoft.AspNet.Server.IIS": "1.0.0-beta3", "Microsoft.AspNet.Mvc": "6.0.0-beta3", "Microsoft.AspNet.Session": "1.0.0-beta3" },推荐答案
将我的评论更改为答案:
Changing my comment to an answer:
在需要会话中间件之前添加会话中间件.否则,它将无法在请求管道中的正确位置使用.
Add the session middleware before anything that needs it. Otherwise it won't be available at the right point in the request pipeline.
实质上,中间件是按顺序运行的-用.UseXyz()添加的每个中间件组件都按顺序运行.因此,如果MVC在会话之前运行,则MVC无法从会话中间件看到会话获取.
The middleware is in essence run sequentially - each middleware component that you add with .UseXyz() is run in order. So if MVC runs before Session, then MVC can't see the session acquisition from the Session middleware.
更多推荐
ASP.NET Core 1.0中的会话
发布评论