AJM
AJM

Reputation: 32490

ASP.NET MVC Temp Data and RedirectToAction in web form

In ASP.NET MVC from one controller in one area I am using:

TempData["Model"] = model;

then RedirectToAction to pass the model to another controller in another area. In the controller action method I immediately pull the data back out of the model.

I am concerned that if I deploy to a web farm then TempData's use of session state will cause issues but am not sure if I can get away with it in this case because I immediately pull the model out of TempData again in the action method I pass to?

Upvotes: 1

Views: 2485

Answers (4)

KevDevMan
KevDevMan

Reputation: 818

Avoid the use of TempData all together. If you are sending your model in the redirection then use something like

RedirectToAction("MyAction", new {model = myModel});

public actionresult MyAction(model model)
{
    /// Mode Code
    return View(MyView, model);
}

Assuming that you controller action will take the model as parameter.

Upvotes: 0

AlexC
AlexC

Reputation: 10756

You are right to be concerned, a RedirectToAction sends the client a 302 message containing a url of the redirected resource. This is then the clients responsibilty to create a new request to the redirected resource. There is no guarantee this resource will be served by the original server. The fact that the request is pulled immediately from TempData makes no difference to this approach, at some point it is going to error.

You need to have some means of managing sessions. You could configure HTTP session affinity so that requests served from a server will always return to the originating server.

You could use cookies for session state or implement a session state provider.

This blog post is also a good start on the overview of the options.

Upvotes: 2

malkam
malkam

Reputation: 2355

Try with cooke based instead of session tempdata

See below link

http://volaresystems.com/Blog/post/2011/06/30/Sessionless-MVC-without-losing-TempData

Upvotes: 0

JuhaKangas
JuhaKangas

Reputation: 883

If you are using InProc session state you might run into problems because in the redirect you could be sent to another server where the same session will not be available.

Two possible options are to either implement a cookie based TempData provider or switch to another session-state mode. Note that cookie based TempData is completely visible to users, though there are implementations where you encrypt the data.

Upvotes: 0

Related Questions