PIPIONE
TempDataDictionary读取中的对象时,该对象将在该请求结束时标记为删除。这意味着如果您在TempData上放一些东西TempData["value"] = "someValueForNextRequest";并且在您访问它的另一个请求中,该值将存在,但是一旦您读取它,该值将被标记为删除://second request, read value and is marked for deletionobject value = TempData["value"];//third request, value is not there as it was deleted at the end of the second requestTempData["value"] == null该Peek和Keep方法,使您无需将其标记为删除读取值。假设我们回到第一个将值保存到TempData的请求。有了Peek该值,而没有通过一次调用将其标记为删除,请参见msdn://second request, PEEK value so it is not deleted at the end of the requestobject value = TempData.Peek("value");//third request, read value and mark it for deletionobject value = TempData["value"];用Keep您指定要保留的标记为删除的密钥。检索对象并随后保存以将其删除,这是两个不同的调用。参见msdn//second request, get value marking it from deletionobject value = TempData["value"];//later on decide to keep itTempData.Keep("value");//third request, read value and mark it for deletionobject value = TempData["value"];您可以Peek在始终希望保留其他请求的值时使用。Keep保留值时使用取决于其他逻辑。关于TempData 在这里和这里的工作方式,您有两个很好的问题希望能帮助到你!