简体   繁体   English

尝试从 TempData 检索时出错

[英]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 .请注意,您试图保存变量sampleDataTempData["sampleDate"]和检索它sampleData It is completely a different key, hence the null reference error.它是完全不同的键,因此出现空引用错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从数据库中检索时出错 - Error when trying to retrieve from database 尝试从Webbrowser检索URL时出现C#异常错误 - c# exception error when trying to retrieve url from webbrowser 尝试从活动目录检索数据时发生操作错误 - An Operations Error Occured When trying to retrieve data from active directory 尝试从Azure检索数据时出现MobileServiceInvalidOperationException - MobileServiceInvalidOperationException When Trying To Retrieve Data From Azure 在 .NET Core MVC 应用程序中使用 TempData 时出现错误 500 - Error 500 when using TempData in .NET Core MVC application 当尝试从数据库中检索数据时,我收到错误“InvalidOperationException未处理” - When trying to retrieve data from the database I get an error “InvalidOperationException was unhandled” 使用tempdata作为错误模型 - Using tempdata as a error model 从控制器传递数据以查看MVC时,Null TempData - Null TempData when passing data from controller to view MVC 尝试检索配置属性时,NullReferenceException是未处理的错误 - NullReferenceException was unhandled error when trying to retrieve config attributes ASP.Net MVC剃刀-在剃刀中写入临时数据并从控件中检索 - ASP.Net MVC Razor - write tempdata in razor and retrieve from control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM