为了账号安全,请及时绑定邮箱和手机立即绑定

ASP.Net Core 2.1 MVC 中的 TempData 始终为 Null

ASP.Net Core 2.1 MVC 中的 TempData 始终为 Null

C#
偶然的你 2021-10-24 14:18:12
我想在我的 .net 核心 mvc 应用程序中使用 TempData。我关注了https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata 中的文章我总是得到 NULL 这是我的代码:public async Task<ActionResult> Index(RentalsFilter filter){    TempData["test"] = "ABC";    return View();}public ActionResult Create(){    var abc = TempData["test"].ToString();    return View();}
查看完整描述

3 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

您是否按照文档中的说明配置了 TempData:


在 ConfigureServices 方法中添加:


services.AddMvc()

    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)

    .AddSessionStateTempDataProvider();


services.AddSession();

在 Configure 方法中,您应该添加:


app.UseSession();


查看完整回答
反对 回复 2021-10-24
  • 3 回答
  • 0 关注
  • 206 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信