[英]Error when trying to retrieve from TempData
I have a C# asp.net MVC application hosted in Azure.我有一个托管在 Azure 中的 C# asp.net MVC 应用程序。 Not always, but sometimes, after a deploy, I face an odd error, that I believe is caused by TempData
.并非总是如此,但有时,在部署后,我会遇到一个奇怪的错误,我认为这是由TempData
引起的。 Restarting the site fixes the issue.重新启动站点可解决此问题。
At the end of a controller method, I have the following pattern:在控制器方法的末尾,我有以下模式:
TempData["sampleData"] = sampleData;
return RedirectToAction("DownloadSampleData"....
Then, at the start of DownloadSampleData
I have:然后,在DownloadSampleData
开始时,我有:
var sampleData = TempData["sampleData"] as SampleDataDto;
The error that is received is:收到的错误是:
Object reference not set to an instance of an object.
And the line it points to being the issue is:它指出的问题是:
var sampleData = TempData["sampleData"] as SampleDataDto;
If the site is restarted, the issue seems to go away.如果站点重新启动,问题似乎就会消失。 This does not happen every time a deploy takes place.这不会在每次部署时发生。
What I am hoping to learn more about here is, how is TempData
initiated?我希望在这里了解更多的是, TempData
是如何启动的? Would I be correct in assuming the error is because TempData
is not available at that point, and therefore causes the Object Reference error?假设错误是因为TempData
不可用,因此导致对象引用错误,我是否正确?
Any thoughts on what could cause this are appreciated as I cannot seem to find anything relevant through Google searches.感谢您对可能导致这种情况的任何想法,因为我似乎无法通过 Google 搜索找到任何相关内容。
Hoping that this is the actual code that you are using.希望这是您正在使用的实际代码。 Please note that you are trying to save variable sampleData
value to TempData["sampleDate"]
and retrieving it from sampleData
.请注意,您试图保存变量sampleData
值TempData["sampleDate"]
和检索它sampleData
。 It is completely a different key, hence the null reference error.它是完全不同的键,因此出现空引用错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.