Asp.net MVC Request Life Cycle
While programming with Asp.net MVC, you should be aware of the life of an Asp.net MVC request from birth to death. In this article, I am going to expose the Asp.net MVC Request Lifecycle. There are six main steps that happen when you make a request to an Asp.net MVC web applications:
Routing
Asp.net Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request’s URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the appropriate handler for that request. Otherwise, the routing engine returns a 404 HTTP status code against that request.When application starts at first time, it registers one or more patterns to the Route Table to tell the routing system what to do with any requests that match these patterns. An application has only one Route Table and this is setup in the Global.asax file of the application.- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute( "Default", // Route name
- "{controller}/{action}/{id}", // URL with parameters
- new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
- );
- }
MvcHandler
The handler returned from the RouteTable will always be an MvcHandler and represents a particular RouteContext. Actually, MvcHandler creates a controller, and also executes the controller.Controller
The controller determines which action method of the controller class to call, and then calls that methodView Result
The action method receives user input, prepares the appropriate response data, and then executes the result by returning a result type. The result type can be ViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.View Engine
Now the ViewResult calls into the current view engine to render the view. Controller is responsible only for preparing the data and decides which view to display by returning a ViewResult instance.View
The data rendered by the current view engine is displayed on the view.
No comments:
Post a Comment