tags:

views:

39

answers:

2

Hi, 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.

+1  A: 

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
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
+1  A: 

Take a look at this question.

Dave Swersky