开发者

Can I use TempData with Response.Redirect?

开发者 https://www.devze.com 2022-12-31 19:50 出处:网络
I am working with ASP.net MVC 2 framework, for multiple sites.We have a base site and th开发者_开发知识库en sub sites that inherit from a \"Core\" site that contains 90% of the functionality that the

I am working with ASP.net MVC 2 framework, for multiple sites. We have a base site and th开发者_开发知识库en sub sites that inherit from a "Core" site that contains 90% of the functionality that the sub sites will use.

In one of the controllers, I am saving some data, adding a UI message to the tempData and then using Response.Redirect.

The redirect works, but the tempdata is empty after the redirect.

I have tried returning "RedirectToAction" and "RedirectToRoute" with the same routing location and while it populates the TempData, the redirect doesn't happen lol..

So I guess in short, is there a way to get tempdata working when using a standard Response.Redirect?


TempData is intended for redirects. But in MVC 2+, reading TempData causes the token to be deleted. So code like this:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消