Unity with ASP.NET

tech1guy used Ask the Experts™
I am using ASP.NET 3.5 SP1 with MVC 1.

Is it possible to use IUnity to retain ViewModel between RedirectToAction() so that I don't have to use TempData to forward the the ViewModel from one action to another. If someone can give me very basic example to accoplish this I will very greateful to you.

Note: I've just one controller and one View but different action. Based upon the actions, I have to display the same View with different updated ViewModel.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
When you say IUnity do you mean unity dependancy injection?

Can you give a more concrete example of what you are actually doing?



Yes, I'm talking of unity dependancy injection. "Microsoft.Practices.Unity.dll". My problem is the following:

* I have a OrderView with form tag. I also have a Controller with one Action which is accepting user's input from the form. Based upon the user input I need to call some web-services and then re-display the same View but with the updated ViewModel. I cannot use PartialView as my whole View will be updated.

I tried to call the web-services and then update the ViewModel and used "return View("OrderView", updatedOrderViewModel);" to re-display the FULL updated View. While doing so I learnt that I have to use RedirectToAction() method in order to display the updated View. But I have to use TempData so that my updatedViewModel can be passed to another action. But as my data is sensitive, I do not want to use TempData.

Therefore, I am thinking that if I use Unity, then I can do good without TempData and also use RedirectToAction().

I've opened number of other questions, but still I've NOT got the response.


Whenever you perform a sucessful http POST you should always redirect to a GET - PRG pattern - http://en.wikipedia.org/wiki/Post/Redirect/Get

I'm not sure I understand why you can't use TempData - the default TempData provider stores the data for one round trip in the server session. Therefore in this configuration your data never leaves the server and never gets transferred to the client. If you are concerned with something on the server getting access to your data then possibly you should encrypt the data while it's in the TempData store - however I'd think in most cases that would be over kill as there will probably be many other hooks in your application which possibly need attention before this.

Therefore something like

public ActionResult UpdateModel(........)
  // Code to update the model

  TempDate["MyTempDataKey"] = model;
  return RedirectToAction("ViewUpdatedModel");

public ActionResult ViewUpdatedModel()
  var model = TempData["MyTempDataKey"];
  return View("MyView",model);

I'm not sure specifically how Unit will help you - apart from being able to inject a service into your controller which could handle storage of the model data in a local database. But it may be simpler to implement your own TempData provider and use that.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.


my concern using TempData is:
First: It is sensitive data and our business unit don't like the idea.
Second: I am concerned about the performance on server as everyone will use TempData on the website. Is it worth to look at the performance or not?

I'm sorry as I am asking a lot, but could you please post a way to encrypt and decrypt tempdata and also comment on the performance.
You certainly need to consider performance - by default session data is stored in the web server's memory so if you've got a lot of users and the data you are storing is quite large then you are likely to consume most of the server's memory. If this is the case then I'd suggest using SQL server to store your session data - this will add an overhead to each request, but it will mean that your application will scale.

In terms of encyrpyting data have a look at articles on encryption - it's a big subject


I'd still suggest that you concentrate on the security of your whole applicartion. A good blog to look at is Barry Dorrans


and the top 10 website vunerabilities are detailed here




Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial