我在操作中初始化了Tempdata,我需要在另一个操作中检索tempdata,但是它返回null。
public IActionResult GetRestaurants(int? id)
{
TempData["HotelID"] = id;
return Ok();
}
[HttpPost]
public IActionResult AddRestaurant()
{
int x =int.Parse(TempData["HotelID"].ToString());
}发布于 2019-10-23 13:24:56
ConfigureServices方法的startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddSession();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}配置startup.cs的方法
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
}更详细的信息可以在ASP.NET内核中的会话和app状态上找到
Index1动作方法
public IActionResult Index()
{
Message = $"Customer abcd added";
TempData["name"] = "Test data";
TempData["age"] = 30;
TempData.Keep();
// Session["name"] = "Test Data";
return View();
}index2动作方法
public IActionResult About()
{
var userName = TempData.Peek("name").ToString();
var userAge = int.Parse(TempData.Peek("age").ToString());
return View();
}https://stackoverflow.com/questions/58522708
复制相似问题