Can I use TempData with Response.Redirect?

I work with ASP.net MVC 2 Framework, for several sites. We have a base site, and then subsidiary sites that inherit from the "main" site, which contains 90% of the functionality that the subsidiary sites will use.

In one of the controllers, I save some data, add a user interface message to tempData, and then use Response.Redirect.

Redirection works, but after redirection tempdata is empty.

I tried to return " RedirectToAction" and " RedirectToRoute" with the same routing location, and although it fills in TempData, the redirect does not occur, lol ..

So, I think in short, is there a way to make tempdata work when using the standard Response.Redirect?

+3
source share
1 answer

TempData is for redirection. But in MVC 2+ reading TempDataleads to the removal of the marker. So, the code is as follows:

if (!string.IsNullOrEmpty(TempData["Foo"].AsString()) { foo =  TempData["Foo"].AsString(); }

... is now broken. But this code:

var bar = TempData["Foo"].AsString();
if (!string.IsNullOrEmpty(bar)) { foo = bar; }

... still works.

+2
source

Source: https://habr.com/ru/post/1746587/


All Articles