HTTP and redirects
Let's first look at how ASP.NET MVC works:
- When an HTTP request arrives, it maps to a set of routes. If the route matches the request, the controller action corresponding to the route will be activated.
- Before calling the action method, ASP.NET MVC binds the model. Model binding is the process of matching the contents of an HTTP request, which is basically text, to the strongly typed arguments of your action method.
Let's also remind ourselves what a redirect is:
An HTTP redirect is a response that a web server can send to a client, telling the client to search for the requested content under a different URL. The new URL is contained in the Location header, which the web server returns to the client. In ASP.NET MVC, you redirect HTTP, returning RedirectResult from the action.
Data transfer
If you simply passed simple values, such as strings and / or integers, you could pass them as query parameters in the URL in the Location header. This is what will happen if you used something like
return RedirectToAction("ActionName", "Controller", new { arg = updatedResultsDocument });
as suggested by others
The reason this will not work is because XDocument is a potentially very complex object. There is no easy way for an ASP.NET MVC framework to serialize a document into something that will fit into the URL, and then bind the model to the value of the URL back to the XDocument action XDocument .
In general, sending a document to the client so that the client sends it back to the server on the next request is a very fragile procedure: this will require all kinds of serialization and deserialization, and all kinds of things may go wrong. If the document is large, it can also become a significant loss of bandwidth and can greatly affect the performance of your application.
Instead, you want to save the document on the server and pass the identifier to the client. The client then passes the identifier along with the next request, and the server retrieves the document using that identifier.
Saving data for search by next query
So now the question is, where does the server store the document in the meantime? Well, it's up to you, and the best choice will depend on your specific scenario. If this document should ultimately be available, you may want to save it to disk or to a database. If it contains only temporary information that is stored in the memory of the web server in the ASP.NET or Session cache (or TempData , which more or less matches the Session at the end), there may be a correct solution. In any case, you store the document under the key, which will allow you to receive the document later:
int documentId = _myDocumentRepository.Save(updatedResultsDocument);
and then you return this key to the client:
return RedirectToAction("UpdateConfirmation", "ApplicationPoolController ", new { id = documentId });
When you want to get a document, you simply retrieve it based on the key:
public ActionResult UpdateConfirmation(int id) { XDocument doc = _myDocumentRepository.GetById(id); ConfirmationModel model = new ConfirmationModel(doc); return View(model); }