在不同的控制器操作方法之间传递数据 [英] Passing data between different controller action methods

查看:29
本文介绍了在不同的控制器操作方法之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ASP.NET MVC 4.我正在尝试将数据从一个控制器传递到另一个控制器.我不明白这一点.我不确定这是否可能?

I'm using ASP.NET MVC 4. I am trying to pass data from one controller to another controller. I'm not getting this right. I'm not sure if this is possible?

这是我要从中传递数据的源操作方法:

Here is my source action method where I want to pass the data from:

public class ServerController : Controller
{
     [HttpPost]
     public ActionResult ApplicationPoolsUpdate(ServiceViewModel viewModel)
     {
          XDocument updatedResultsDocument = myService.UpdateApplicationPools();

          // Redirect to ApplicationPool controller and pass
          // updatedResultsDocument to be used in UpdateConfirmation action method
     }
}

我需要将它传递给这个控制器中的这个操作方法:

I need to pass it to this action method in this controller:

public class ApplicationPoolController : Controller
{
     public ActionResult UpdateConfirmation(XDocument xDocument)
     {
          // Will add implementation code

          return View();
     }
}

我在 ApplicationPoolsUpdate 操作方法中尝试了以下操作,但它不起作用:

I have tried the following in the ApplicationPoolsUpdate action method but it doesn't work:

return RedirectToAction("UpdateConfirmation", "ApplicationPool", new { xDocument = updatedResultsDocument });

return RedirectToAction("UpdateConfirmation", new { controller = "ApplicationPool", xDocument = updatedResultsDocument });

我将如何实现这一目标?

How would I achieve this?

推荐答案

HTTP 和重定向

让我们首先回顾一下 ASP.NET MVC 的工作原理:

Let's first recap how ASP.NET MVC works:

  1. 当一个 HTTP 请求进来时,它会与一组路由进行匹配.如果路由与请求匹配,则将调用与该路由对应的控制器操作.
  2. 在调用 action 方法之前,ASP.NET MVC 执行模型绑定.模型绑定是将 HTTP 请求的内容(基本上只是文本)映射到操作方法的强类型参数的过程

让我们也提醒自己什么是重定向:

Let's also remind ourselves what a redirect is:

HTTP 重定向是网络服务器可以发送给客户端的响应,告诉客户端在不同的 URL 下查找请求的内容.新 URL 包含在网络服务器返回给客户端的 Location 标头中.在 ASP.NET MVC 中,您通过从操作返回 RedirectResult 来执行 HTTP 重定向.

An HTTP redirect is a response that the webserver can send to the client, telling the client to look for the requested content under a different URL. The new URL is contained in a Location header that the webserver returns to the client. In ASP.NET MVC, you do an HTTP redirect by returning a RedirectResult from an action.

传递数据

如果您只是传递简单的值,如字符串和/或整数,您可以将它们作为查询参数传递到 Location 标头中的 URL 中.如果你使用类似

If you were just passing simple values like strings and/or integers, you could pass them as query parameters in the URL in the Location header. This is what would happen if you used something like

return RedirectToAction("ActionName", "Controller", new { arg = updatedResultsDocument });

正如其他人所建议的

这不起作用的原因是 XDocument 可能是一个非常复杂的对象.ASP.NET MVC 框架没有直接的方法将文档序列化为适合 URL 的内容,然后将模型绑定从 URL 值返回到您的 XDocument 操作参数.

The reason that this will not work is that the XDocument is a potentially very complex object. There is no straightforward way for the ASP.NET MVC framework to serialize the document into something that will fit in a URL and then model bind from the URL value back to your XDocument action parameter.

通常,将文档传递给客户端以便客户端在下一次请求时将其传递回服务器是一个非常脆弱的过程:它需要各种序列化和反序列化,并且各种事情都可能出问题.如果文档很大,也可能会大量浪费带宽,并可能严重影响应用程序的性能.

In general, passing the document to the client in order for the client to pass it back to the server on the next request, is a very brittle procedure: it would require all sorts of serialisation and deserialisation and all sorts of things could go wrong. If the document is large, it might also be a substantial waste of bandwidth and might severely impact the performance of your application.

相反,您要做的是将文档保留在服务器上,并将标识符传递回客户端.然后客户端将标识符与下一个请求一起传递,服务器使用此标识符检索文档.

Instead, what you want to do is keep the document around on the server and pass an identifier back to the client. The client then passes the identifier along with the next request and the server retrieves the document using this identifier.

存储数据以供下次请求检索

那么,现在的问题变成了,服务器在此期间将文档存储在哪里?好吧,这由您决定,最佳选择将取决于您的特定情况.如果需要长期使用此文档,您可能希望将其存储在磁盘或数据库中.如果它只包含瞬态信息,则将其保存在 Web 服务器的内存、ASP.NET 缓存或 Session(或 TempData,与Session 最后)可能是正确的解决方案.无论哪种方式,您都可以将文档存储在一个密钥下,以便您以后检索该文档:

So, the question now becomes, where does the server store the document in the meantime? Well, that is for you to decide and the best choice will depend upon your particular scenario. If this document needs to be available in the long run, you may want to store it on disk or in a database. If it contains only transient information, keeping it in the webserver's memory, in the ASP.NET cache or the Session (or TempData, which is more or less the same as the Session in the end) may be the right solution. Either way, you store the document under a key that will allow you to retrieve the document later:

int documentId = _myDocumentRepository.Save(updatedResultsDocument);

然后将该密钥返回给客户端:

and then you return that key to the client:

return RedirectToAction("UpdateConfirmation", "ApplicationPoolController ", new { id = documentId });

当你想检索文档时,你只需根据键来获取它:

When you want to retrieve the document, you simply fetch it based on the key:

 public ActionResult UpdateConfirmation(int id)
 {
      XDocument doc = _myDocumentRepository.GetById(id);

      ConfirmationModel model = new ConfirmationModel(doc);

      return View(model);
 }

这篇关于在不同的控制器操作方法之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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