TempData的保持()与PEEK() [英] TempData keep() vs peek()

查看:1841
本文介绍了TempData的保持()与PEEK()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是保持()和peek()?

What is the difference between keep() and peek()?

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.

我不能让真正的区别是什么,难道他们都保持一个值,另一个请求?

I can't get really what the difference is, don't they both keep a value for another request?

推荐答案

当在一个对象 TempDataDictionary 被读取,这将在该月底被标记为删除请求。

When an object in a TempDataDictionary is read, it will be marked for deletion at the end of that request.

这意味着,如果你把TempData的类似<​​/ P>

That means if you put something on TempData like

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

皮克保留方法让你无标记为删除读取值。假设我们回到那里的值保存到TempData的第一个请求。

The Peek and Keep methods 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.

使用皮克你得到的值,而不使用单个呼叫标记为删除,请参阅msdn:

With Peek you get the value without marking it for deletion with a single call, see 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"];

使用保留您指定被标记为要保留删除的关键。检索对象,后来从删除保存有2个不同的电话。请参见 MSDN

With Keep you 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

//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 Peek when you always want to retain the value for another request. Use Keep when retaining the value depends on additional logic.

您有大约2怎么TempData的作品<一个很好的问题href=\"http://stackoverflow.com/questions/12815739/tempdata-wont-destroy-after-second-request\">here和<一个href=\"http://stackoverflow.com/questions/14891744/tempdata-implementation-changes-reasons-for-the-change\">here

You have 2 good questions about how TempData works here and here

希望它帮助!

这篇关于TempData的保持()与PEEK()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆