Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a new controller instance for each request?

Tags:

asp.net-mvc

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.

like image 796
Trimack Avatar asked Mar 17 '10 16:03

Trimack


People also ask

Which is a new instance created for every HTTP request?

A Controller is created for every request by the ControllerFactory (which by default is the DefaultControllerFactory ).

Is controller same as API?

The main difference is: Web API is a service for any client, any devices, and MVC Controller only serve its client.

How do I add a controller in Visual Studio?

Using the Add Controller Menu Option The easiest way to create a new controller is to right-click the Controllers folder in the Visual Studio Solution Explorer window and select the Add, Controller menu option (see Figure 1). Selecting this menu option opens the Add Controller dialog (see Figure 2).

What is a controller in ASP?

Controllers are essentially the central unit of your ASP.NET MVC application. It is the 1st recipient, which interacts with incoming HTTP Request. So, the controller decides which model will be selected, and then it takes the data from the model and passes the same to the respective view, after that view is rendered.


1 Answers

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().

like image 101
womp Avatar answered Nov 08 '22 15:11

womp