Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Controller Lifecycle

Tags:

c#

asp.net-mvc

It's my understanding that the constructor for a controller is not called during each web request. Assuming this is true, what is the lifecycle of a controller? Is is "constructed" upon app start, then cached and invoked with the requestcontext injected into it with each web request?

Just to be clear, I'm not asking how to emulate constructor behavior, I use the OnActionExecuting event to initiate things I would normally do in a constructor. Also, I do use constructors on controllers for unit and system testing.

Thanks!

like image 511
Josh Pearce Avatar asked Nov 19 '09 14:11

Josh Pearce


People also ask

What is the life cycle of ASP.NET MVC?

MVC actually defined in two life cycles, the application life cycle, and the request life cycle. The Starting point for every MVC application begins with routing. After that, the received request figures out and finds how it should be handled with the help of the URL Routing Module.

How does the page lifecycle of ASP.NET MVC works?

The ASP.NET MVC Process. In a MVC application, no physical page exists for a specific request. All the requests are routed to a special class called the Controller. The controller is responsible for generating the response and sending the content back to the browser.

Which handles the initial step in the ASP.NET MVC request life cycle?

Asp.net Routing is the first step in MVC request cycle.

What is the life cycle of ASP NET core?

The ASP.NET Core MVC Request Life Cycle is a sequence of events, stages or components that interact with each other to process an HTTP request and generate a response that goes back to the client. In this article, we will discuss each and every stage of ASP.NET Core MVC Request Life Cycle in detail.


1 Answers

If you use the default controller factory a new instance will be constructed for each request and that's the way it should be. Controllers shouldn't be shared among different requests. You could though write a custom factory that manages the lifetime of the controllers.

like image 86
Darin Dimitrov Avatar answered Sep 18 '22 22:09

Darin Dimitrov