3

If we want to create some objects to be used by all action methods in a controller, can we store them as instance variables in the controller?

Phil Haack mentions that controllers are not meant to be reused in this old post: Asp.Net MVC Beta: Previous RouteData overrides current RouteData?

But is this one-controller-per-request behavior guaranteed?

I don't want to be in a situation where a reused controller has data from another request.

Community
  • 1
  • 1
Pete
  • 1,790
  • 2
  • 19
  • 31

3 Answers3

4

Yes (to the question in your title) and No (to the question in your post).

A new controller instance is created for each request. You might be able to do this by using your own controller factory that caches controllers and only creates them as needed, but I wouldn't recommend it. You'd be better off to simply cache any information that is needed either in the Session or the Cache or store the information in the View (hidden, if necessary) than to go to the trouble of creating a new controller factory.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • To clarify, I'm trying to understand the best way to create some data to be shared by many action methods in one place. I was trying to figure out the best way to pass data from OnActionExecuting into action methods. I didn't realize that we could populate ViewData on controllerContext, which seems like it makes more sense than instance variables inside the controller. I think my question is answered: Yes, only one controller per request. – Pete Nov 04 '09 at 18:19
  • Which is best way for ***performance*** in a website with more 4000 concurrents ? ***Session***, ***Cache***, ***Application*** ? – Kiquenet Jun 22 '16 at 10:40
1

Every time you call an action method, a new instance of the controller class should be created. So it is not a good idea to have your action methods depend on instance variables. If you use the DefaultControllerFactory then you will get a new controller instance for each request. If you use a custom controller factory you could override this behavior, which I wouldn't recommend.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

You can use TempData to store temporary data in your controller

Gregoire
  • 24,219
  • 6
  • 46
  • 73
  • 1
    Doesn't TempData live until the subsequent request too? – Pete Nov 04 '09 at 18:20
  • The OP explicitly specified I quote: `I don't want to be in a situation where a reused controller has data from another request.`. Using TempData will do just that: reuse data from previous request. – Darin Dimitrov Nov 04 '09 at 18:33
  • When you say `actions` this means multiple requests (request per action), maybe you mean for the `current session/user`. – Darin Dimitrov Nov 04 '09 at 18:41
  • I will remove the negative vote though from your answer because the OP didn't actually specify if he meant different requests or different requests from the same user. – Darin Dimitrov Nov 04 '09 at 18:43
  • what I wanted to mean is that the data in tempData persist if in a request to a specific action there is a RedirectToAction method. After that yes, perhaps I was wrong. – Gregoire Nov 04 '09 at 18:53