ASP.NET Core 1.0中的会话

编程入门 行业动态 更新时间:2024-10-26 23:29:04
本文介绍了ASP.NET Core 1.0中的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在我的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中的会话

本文发布于:2023-11-05 07:21:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560243.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!