问题描述
我有一个类似于下面的控制器操作的东西,TempData的被我的框架进行初始化。我注意到,TempData的不清除出值,一旦它的读取显示在行动EmployeeUnderAge。
当它的TempData清除已读取的数据?
公共类HomeController的:控制器
{
公众的ActionResult指数(INT标识)
{
VAR employeeAge =(INT)的TempData [时代];
RouteData.Values.Add(时代,employeeAge);
返回RedirectToAction(EmployeeUnderAge);
} 公众的ActionResult EmployeeUnderAge(INT employeeAge)
{
VAR stillInTempData =(employeeAge ==((INT)的TempData [时代]));
返回(stillInTempData)?查看(「指数」):视图(错误);
}
}
下面是一些关键点的温度使用数据时要注意。
-
一个读访问临时数据不会删除立即从字典中的项目,但只有标记为删除。
-
的TempData不会总是删除已读出的项目。它只是删除项目时的动作导致一个HTTP 200(OK)状态code(即:的ViewResult / JsonResult / ContentResult类型等)
-
在的导致在HTTP 302(如任何重定向的动作),该数据被保持在即使当被访问这是在我的问题的情况下存储。动作的情况下TempData的显然是专为将数据传递到不同的控制器/动作,因此重定向时不清理掉是有道理的。
I have a controller action something similar to the below, TempData was initialized by my framework. I've noticed that TempData doesn't clear out the value once it is read as shown in the action "EmployeeUnderAge".
When does TempData clears the data that has been read?
public class HomeController : Controller
{
public ActionResult Index(int iD)
{
var employeeAge = (int)TempData["Age"];
RouteData.Values.Add("Age", employeeAge);
return RedirectToAction("EmployeeUnderAge");
}
public ActionResult EmployeeUnderAge(int employeeAge)
{
var stillInTempData = (employeeAge == ((int) TempData["Age"]));
return (stillInTempData) ? View("Index") : View("Error");
}
}
Below are some of the key points to note when using Temp data.
A read access to temp data doesn't remove items from the dictionary immediately, but only marks for deletion.
TempData will not always remove the item that has been read. It only removes the item when an action results in an HTTP 200 (OK) status code (ie: ViewResult/JsonResult/ContentResult etc)
In case of actions that result in an HTTP 302 (such as any redirect actions), the data is retained in storage even when it is accessed which is the case in my question. TempData apparently is designed for passing data to different controllers/actions and hence doesn't clearing out during redirects is justified
这篇关于ASP.NET TempData的是不是看完后甚至被清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!