【发布时间】:2015-08-09 14:52:24
【问题描述】:
我发现我不应该在 ASP MVC here、here 和其他地方大量使用 Session。
所以,我想知道像我在下面那样使用TempData 是否更好。
public ActionResult Action1()
{
if (SomeCondition)
{
/*
I want to show alert to user based on this value that should appear in Action2 view
So, is it better to:
1. Session["user"] = "something";
2. TempData["user"] = "something";
*/
return RedirectToAction("Action2");
}
return View();
}
public ActionResult Action2()
{
/*
1. I can read Session["user"] in the view
2. TempData["user"] = TempData["user"].ToString();
Now I can read TempData in the view
*/
return View();
}
【问题讨论】:
-
它取决于你是/必须坚持的what。您甚至可以这样做客户端(同样,取决于您需要坚持什么)。
-
我的意思是其中一个在这种情况下的性能是否更好,但我从答案中了解到
TempData基本上是Session。所以我认为没有区别,或者可能 - 猜测 - 即使使用 2TempData会比Session更糟糕,因为服务器会创建一个,然后终止它,然后创建另一个,然后终止它,而不是创建Session然后超时后过期。 -
同样,它取决于你需要持久化什么样的数据 ....
标签: c# asp.net-mvc session