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

坚强是说给别人听的谎言 提交于 2020-01-15 03:54:06

问题


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:

  • The HttpContext and its HttpContext.Items dictionary.
  • Services added with AddScoped via dependency injection.
  • The TempData dictionary? (not so sure about that)

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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/40553576/asp-net-core-mvc-what-happens-to-a-request-on-redirecttoaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!