ASP .NET Core MVC:RedirectToAction上的请求将如何处理 [英] ASP .NET Core MVC: What happens to a request on RedirectToAction

查看:519
本文介绍了ASP .NET Core MVC: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.

在ASP .NET Core的整个文档中,他们使用请求"一词.我当时以为这是WebServer客户端发出的HttpRequest.

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:

  • HttpContext及其HttpContext.Items字典.
  • 通过依赖项注入与AddScoped一起添加的服务.
  • TempData词典? (对此不太确定)
  • The HttpContext and its HttpContext.Items dictionary.
  • Services added with AddScoped via dependency injection.
  • The TempData dictionary? (not so sure about that)

但是当尝试传递数据时,我观察到,当我执行return RedirectToAction(...);时,HttpContext更改(HttpContext.GetHashCode()具有不同的值),TempData更改和通过AddScoped添加的服务也新对象.

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.

这表明在RedirectToAction上发出了一个新请求,再次执行了请求管道的所有步骤.但我的期望是RedirectToAction仅使用不同的控制器动作继续当前请求管道.

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.

那么在控制器操作中调用RedirectToAction并返回结果时,实际发生了什么?

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

更新: 使用TempData可以,但是必须首先配置TempDataProvider.例如,将services.AddSingleton<ITempDataProvider,SessionStateTempDataProvider>();添加到Startup.cs.谢谢@RonC.

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

推荐答案

如前所述,RedirecToAction将导致浏览器发出新请求,并且当该新请求进入时,它将创建一个全新的HttpContext .如前所述,要在两个请求之间传递数据,可以使用查询字符串,会话或cookie.但是,还有另一种可供考虑的选择.

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
数据可以通过TempData集合从一个请求传递到另一个请求,该集合可以在控制器操作方法中访问. TempData集合专门用于将数据从一个请求传递到另一个请求. TempData的优点在于,放置在TempData中的对象的生存期恰好是另外一个请求.因此,在请求1中放置在TempData中的任何内容都将存在于请求2中,但是在请求2结束时会自动从TempData中删除.这使得TempData非常适合将数据从一个请求传递到另一个请求而不必公开查询字符串中的信息,或者可能会在会话中忘记该信息并使会话对象膨胀.

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.

这篇关于ASP .NET Core MVC:RedirectToAction上的请求将如何处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆