Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net Mvc - does onactionexecuted get called before or after ActionResult.Execute?

Tags:

asp.net-mvc

Does Controller.OnActionExecuted get called before or after ActionResult.Execute?

Is there a timeline somewhere of the order in which events occur? I can't find anything with google-fu alone.

like image 236
George Mauer Avatar asked Aug 28 '12 01:08

George Mauer


2 Answers

The Controller.OnActionExecuted gets called first.

See this post on MSDN, it covers the controller pipeline for MVC.

  1. Receive first request for the application
  2. Perform routing
  3. Create MVC request handler
  4. Create controller
  5. Execute controller
  6. Invoke action
  7. Execute result
like image 63
Mark Oreta Avatar answered Sep 21 '22 20:09

Mark Oreta


Below snapshot shows how execution take Place

http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/understanding-action-filters-cs

Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.

Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.

MVC Life Cycle

Image Courtesy: - http://www.dotnetinterviewquestions.in/article_explain-mvc-application-life-cycle_210.html

Details of Article :- https://www.codeproject.com/Articles/556995/ASP-NET-MVC-interview-questions-with-answers

Any web application has two main execution steps first understanding the request and depending on the type of the request sending out appropriate response. MVC application life cycle is not different it has two main phases first creating the request object and second sending our response to the browser.

Creating the request object: -The request object creation has four major steps. Below is the detail explanation of the same.

Step 1 Fill route: - MVC requests are mapped to route tables which in turn specify which controller and action to be invoked. So if the request is the first request the first thing is to fill the route table with routes collection. This filling of route table happens in the global.asax file.

Step 2 Fetch route: - Depending on the URL sent “UrlRoutingModule” searches the route table to create “RouteData” object which has the details of which controller and action to invoke.

Step 3 Request context created: - The “RouteData” object is used to create the “RequestContext” object.

Step 4 Controller instance created: - This request object is sent to “MvcHandler” instance to create the controller class instance. Once the controller class object is created it calls the “Execute” method of the controller class.

Creating Response object: - This phase has two steps executing the action and finally sending the response as a result to the view.

like image 30
Saineshwar Avatar answered Sep 20 '22 20:09

Saineshwar