Hi @Volk Volk ,
First, based on your description, I create an Asp.Net 6 MVC application to Test the TempData
and LocalRedirect
method, everything works well.
It seems that LocalRedirect sometimes clears TempData during the transition, and sometimes does not have time to do this
Second, about the TempData lost issue, I suggest you can pay attention the following points:
- The TempData providers using either cookies or session state. You can use F12 developer tools to check whether the cookies or session is expired.
- TempData stores the data temporarily and automatically removes it after retrieving a value. The data persists only from one request to the next (such as transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller), unless you mark one or more keys for retention by using the Keep method. So, after redirect the returnUrl, whether it will send another request or not? If you make another redirect, try to use the Keep method.
More detail information about TempData, see Session and state management in ASP.NET Core
Can you tell me what I need to do so that TempData["alert"] is always correctly passed when using LocalRedirect?
To correctly pass the data when using the LocalRedirect, you can try to store the data in the Database or Azure Key Vault.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion