FSMaxB
FSMaxB

Reputation: 2490

ASP .NET Core MVC: What happens to a request on RedirectToAction

I was trying to pass data around between controllers all day long, but now I'm at the point where I think I haven't quite understood the basics.

Throughout the documentation of ASP .NET core, they use the word "request". I was under the assumption that this is the HttpRequest that is made by the client of the WebServer.

There are also different things that are supposed to be bound to the lifetime of a request:

But when trying to pass data around, I made the observation that when I do return RedirectToAction(...); the HttpContext changes (HttpContext.GetHashCode() has a different value), TempData changes and services added via AddScoped are also new objects.

That would suggest that on RedirectToAction a new request is made, going through all the steps of the request pipeline again. My expectation though was that a RedirectToAction only continues the current request pipeline with a different controller action.

I also thought that the browser or whatever client only made one request and got one response during that entire process.

So what is actually happening when calling RedirectToAction in a controller action and returning the result?

UPDATE: Using TempData works, but a TempDataProvider has to be configured first. For example add services.AddSingleton<ITempDataProvider,SessionStateTempDataProvider>(); to Startup.cs. Thanks @RonC.

Upvotes: 2

Views: 2584

Answers (2)

RonC
RonC

Reputation: 33851

As mentioned, RedirecToAction will cause the browser to make a new request, and when that new request comes in, it will create a totally new HttpContext. As mentioned, To pass data between the two requests, you can use the query string, session or cookies. But there is another option to consider.

TempData
Data can be passed from one request to another via the TempData collection which is accessible in the controller action method. The TempData collection was specifically designed for passing data from one request to another. The beauty of TempData is that the lifetime of an object placed in TempData is exactly one additional request. So anything placed in TempData in request 1 will be there for request 2 but then be automatically removed from TempData at the conclusion of request 2. This makes TempData perfect for passing data from one request to another without having to disclose that information in a query string or possibly forgetting it in session and bloating the session object.

Upvotes: 2

Paweł Hemperek
Paweł Hemperek

Reputation: 1180

It's impossible to save state of current request, because... HTTP is stateless. Every RedirectToAction really tells browser to make another HTTP request. As documentation says.

Returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action.

If you would like to pass some data between HTTP requests, you have to use cookies or session mechanism.

Upvotes: 1

Related Questions