开发者

Can I use TempData with Response.Redirect?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜