10

I am just wondering, is there a new controller instance for each request? How does this actually work? I read something like that after an incoming request routing selects correct controller.. and I guess it creates a new instance of that and then the controller handles the request further. If that is so, what about actions redirecting to other actions? Does that initiate new routing process and new instance of controller, too?

Thanks in advance.

Trimack
  • 4,153
  • 9
  • 44
  • 70

1 Answers1

16

Yes, a new instance in instantiated for each request, and destroyed at the end of the request.

Each route is handled by an instance of an MvcRouteHandler. The default handler calls into the ControllerFactory, which, based on the url tokens, instantiates a new controller via a reflection call to Activator.CreateInstance().

womp
  • 115,835
  • 26
  • 236
  • 269
  • In fact, as of MVC 2, the framework will try to detect if you've incorrectly set up your DI container to return controllers with non-transient lifetimes. If so, it will throw an exception asking you to reconfigure your DI container. – Levi Mar 17 '10 at 17:15