asp.net-mvc TempData keep() 与 peek()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21252888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TempData keep() vs peek()
提问by Ghasan
What is the difference between keep() and peek()?
keep() 和 peek() 有什么区别?
MSDN says:
MSDN 说:
- keep():
marks the specified key in the dictionary for retention. - peek():
returns an object that contains the element that is associated with the specified key, without marking the key for deletion.
- 保持():
marks the specified key in the dictionary for retention. - 窥视():
returns an object that contains the element that is associated with the specified key, without marking the key for deletion.
I can't get really what the difference is, don't they both keep a value for another request?
我真的不明白有什么区别,他们不是都为另一个请求保留了一个值吗?
回答by Daniel J.G.
When an object in a TempDataDictionaryis read, it will be marked for deletion at the end of that request.
当 a 中的对象TempDataDictionary被读取时,它将在该请求结束时被标记为删除。
That means if you put something on TempData like
这意味着如果你在 TempData 上放一些东西,比如
TempData["value"] = "someValueForNextRequest";
And on another request you access it, the value will be there but as soon as you read it, the value will be marked for deletion:
在您访问它的另一个请求中,该值将在那里,但一旦您阅读它,该值将被标记为删除:
//second request, read value and is marked for deletion
object value = TempData["value"];
//third request, value is not there as it was deleted at the end of the second request
TempData["value"] == null
The Peekand Keepmethods allow you to read the value without marking it for deletion. Say we get back to the first request where the value was saved to TempData.
该Peek和Keep方法,使您无需将其标记为删除读取值。假设我们回到将值保存到 TempData 的第一个请求。
With Peekyou get the value without marking it for deletion with a single call, see msdn:
随着Peek你得到的价值,而不具有单一通话将其标记为删除,请参阅MSDN:
//second request, PEEK value so it is not deleted at the end of the request
object value = TempData.Peek("value");
//third request, read value and mark it for deletion
object value = TempData["value"];
With Keepyou specify a key that was marked for deletion that you want to keep. Retrieving the object and later on saving it from deletion are 2 different calls. See msdn
与Keep您指定要保留的标记为删除的密钥。检索对象并稍后将其从删除中保存是 2 个不同的调用。见msdn
//second request, get value marking it from deletion
object value = TempData["value"];
//later on decide to keep it
TempData.Keep("value");
//third request, read value and mark it for deletion
object value = TempData["value"];
You can use Peekwhen you always want to retain the value for another request. Use Keepwhen retaining the value depends on additional logic.
Peek当您总是希望为另一个请求保留值时,您可以使用。Keep保留值时使用取决于附加逻辑。
You have 2 good questions about how TempData works hereand here
您有 2 个关于 TempData 如何在此处和此处工作的好问题
Hope it helps!
希望能帮助到你!
回答by Shivprasad Ktheitroadala
Just finished understanding Peek and Keep and had same confusion initially. The confusion arises becauses TempData behaves differently under different condition. You can watch this video which explains the Keep and Peek with demonstration https://www.facebook.com/video.php?v=689393794478113
刚刚完成对 Peek 和 Keep 的理解,最初也有同样的困惑。出现混淆是因为 TempData 在不同条件下的行为不同。您可以观看此视频,该视频通过演示解释了 Keep 和 Peek https://www.facebook.com/video.php?v=689393794478113
Tempdata helps to preserve values for a single request and CAN ALSO preserve valuesfor the next request depending on 4 conditions”.
Tempdata 有助于保存单个请求的值,并且还可以根据4 个条件保存下一个请求的值”。
If we understand these 4 points you would see more clarity.Below is a diagram with all 4 conditions, read the third and fourth point which talks about Peek and Keep.
如果我们理解了这 4 点,你会看得更清楚。下面是一个包含所有 4 个条件的图表,阅读第三和第四点,其中谈到了 Peek 和 Keep。


Condition 1 (Not read):-If you set a “TempData” inside your action and if you do not read it in your view then “TempData” will be persisted for the next request.
条件 1(未读取):-如果您在操作中设置了“TempData”,并且如果您没有在视图中读取它,那么“TempData”将在下一个请求中保留。
Condition 2 ( Normal Read) :-If you read the “TempData” normally like the below code it will not persist for the next request.
条件 2(正常读取):-如果您像下面的代码一样正常读取“TempData”,它将不会在下一个请求中持续存在。
string str = TempData["MyData"];
Even if you are displaying it's a normal read like the code below.
即使您正在显示它,它也是像下面的代码一样的正常读取。
@TempData["MyData"];
Condition 3 (Read and Keep) :-If you read the “TempData” and call the “Keep” method it will be persisted.
条件 3(读取并保持):-如果您读取“TempData”并调用“Keep”方法,它将被持久化。
@TempData["MyData"];
TempData.Keep("MyData");
Condition 4 ( Peek and Read) :-If you read “TempData” by using the “Peek” method it will persist for the next request.
条件 4(Peek 和 Read):-如果您使用“Peek”方法读取“TempData”,它将为下一个请求保留。
string str = TempData.Peek("Td").ToString();
Reference :- http://www.codeproject.com/Articles/818493/MVC-Tempdata-Peek-and-Keep-confusion
参考:- http://www.codeproject.com/Articles/818493/MVC-Tempdata-Peek-and-Keep-confusion
回答by Anil Singh
TempDatais also a dictionary object that stays for the time of an HTTP Request. So, TempData can be used to maintain data between one controller action to the other controller action.
TempData也是一个字典对象,在 HTTP 请求期间保留。因此,TempData 可用于维护一个控制器操作与另一个控制器操作之间的数据。
TempDatais used to check the null values each time. TempData contain two method keep() and peek() for maintain data state from one controller action to others.
TempData用于每次检查空值。TempData 包含两个方法 keep() 和 peek() 用于维护从一个控制器操作到其他控制器操作的数据状态。
When TempDataDictionaryobject is read, At the end of request marks as deletion to current read object.
当 读取TempDataDictionary对象时,在请求结束时将当前读取的对象标记为删除。
The keep()and peek()method is used to read the data without deletion the current read object.
的保持()和PEEK()方法用于无缺失读取数据中的当前读取的对象。
You can use Peek()when you always want to hold/prevent the value for another request. You can use Keep()when prevent/hold the value depends on additional logic.
当您总是想保留/阻止另一个请求的值时,您可以使用Peek()。当阻止/保持值取决于附加逻辑时,您可以使用Keep()。
Overloading in TempData.Peek() & TempData.Keep()as given below.
如下所示在 TempData.Peek() 和 TempData.Keep() 中重载。
TempData.Keep()have 2 overloaded methods.
TempData.Keep()有 2 个重载方法。
void keep(): That menace all the data not deleted on current request completion.
void keep(string key): persist the specific item in TempData with help of name.
void keep():这会威胁到当前请求完成时未删除的所有数据。
void keep(string key):在名称的帮助下将特定项目保留在 TempData 中。
TempData.Peek()no overloaded methods.
TempData.Peek()没有重载方法。
- object peek(string key): return an object that contain items with specific key without making key for deletion.
- object peek(string key): 返回一个包含具有特定键的项目的对象,而不需要删除键。
Example for return type of TempData.Keep() & TempData.Peek() methods as given below.
TempData.Keep() 和 TempData.Peek() 方法的返回类型示例如下所示。
public void Keep(string key) { _retainedKeys.Add(key); }
public object Peek(string key) { object value = values; return value; }
public void Keep(string key) { _retainedKeys.Add(key); }
公共对象Peek(字符串键){ 对象值 = 值;返回值;}
回答by Zabavsky
don't they both keep a value for another request?
他们不是都为另一个请求保留一个值吗?
Yes they do, but when the first one is void, the second one returns and object:
是的,他们这样做,但是当第一个是 时void,第二个返回并且object:
public void Keep(string key)
{
_retainedKeys.Add(key); // just adds the key to the collection for retention
}
public object Peek(string key)
{
object value;
_data.TryGetValue(key, out value);
return value; // returns an object without marking it for deletion
}
回答by Cd Patel
Keep() method marks the specified key in the dictionary for retention
Keep() 方法将字典中的指定键标记为保留
You can use Keep() when prevent/hold the value depends on additional logic.
当阻止/保持值取决于附加逻辑时,您可以使用 Keep()。
when you read TempData one's and want to hold for another request then use keep method, so TempData can available for next request as above example.
当您读取 TempData 并希望保留另一个请求时,请使用 keep 方法,因此 TempData 可用于下一个请求,如上例所示。

