donet Core 中使用Seesion
Asp.net core 和Asp.net framework 区别
- Asp.net core
- 轻量级 只提供最基础的“骨架”;仅仅能维持“生活”;
- 可以选配“组件”
- 减少开销
- Asp.net framework
- 强抢强卖。直接梭哈 全部给你
- 会有很多自己没有用到的功能
Asp.net core 不在内置 Session 需要开发主要的配置
直接使用的话 报错图如下
An unhandled exception occurred while processing the request.

代码
1 public IActionResult Index() 2 { 3 //Asp.Net Core 的几种页面传值方式 4 //1.ViewData 5 ViewData["viewData"] = "使用 ViewData 进行页面传值"; 6 //2. ViewBag 7 ViewBag.name = "使用viewBag.name 进行页面传值"; 8 //3.TempData 9 TempData["TempData"] = "使用TempData 进行页面传值"; 10 //4.使用View(object)传值 11 //注意:Viwe(string)有重载方法 12 object str = "使用object View(str) 进行传值"; 13 14 //5.使用Seession 进行传值 15 //注意:Asp.net Core中没有内置Seession 16 //需要主动配置 激活使用 17 18 string seesionvalue =HttpContext.Session.GetString("session"); 19 if (string.IsNullOrWhiteSpace(seesionvalue)) { 20 HttpContext.Session.SetString("session", "使用Seesion进行传值"); 21 } 22 23 24 return View(str); 25 }
解决方法/正确的使用方法:
注册session:
在Startup.cs文件中的ConfigureServices方法中添加:
services.AddSession();
在Startup.cs文件中的Configure方法中加上这一句代码
app.UseSession();

浙公网安备 33010602011771号