I have a C# asp.net MVC application hosted in Azure. Not always, but sometimes, after a deploy, I face an odd error, that I believe is caused by 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:
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? Would I be correct in assuming the error is because TempData
is not available at that point, and therefore causes the Object Reference error?
Any thoughts on what could cause this are appreciated as I cannot seem to find anything relevant through Google searches.
Hoping that this is the actual code that you are using. Please note that you are trying to save variable
sampleData
value toTempData["sampleDate"]
and retrieving it fromsampleData
. It is completely a different key, hence the null reference error.