MVC Pipeline

mvcpipeline

MVC Pipeline

0.First Request

When the application starts up, like any ASP.NET application, it runs Global.asax’s Application_Start() method.In this method, you can add Route objects to the static RouteTable.

void Application_Start(object sender, EventArgs e)
{
   RouteConfig.RegisterRoutes(RouteTable.Routes);
}

1

1.Incoming Request & Routing

The incoming request from IIS pipeline is handed over to URL Routing module MvcHandler then MvcHandler  calls Routes.GetRouteData() which matches the incoming request against the list of Route objects added in Application_Start() which analyses the request and looks up Routing table to figure out which controller the incoming request maps to.If the route is found in the routing table . IRouteHandler's GetHttpHandler() is called, returning an IHttpHandler, whose ProcessRequest() method is finally invoked MVCRouteHandler executes and brings the instance of MVCHttpHandler . Together they act as a gateway into the MVC Framework.

Routing : Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern. MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table.

Route Objects : Collection of Routes.routes

Route Table :The RouteTable is the one that stores all the routes defined in the application. The RouteTable contains property named Routes which is of type RouteCollection and it is where we add all our routes. From the Application_Start we pass this RouteCollection to the RegisterRoutes method.

HTTP Methods URI Path Action
GET /OrderManagement/OrderBooking/OrderBooking RetriveAllOrders
GET /OrderManagement/OrderBooking/OrderBooking/Id RetriveOrderByOrderId
PUT /OrderManagement/OrderBooking/OrderBooking/Id InsertOrderByOrderId
DELETE /OrderManagement/OrderBooking/OrderBooking/Id DeleteOrderByOrderId
POST /OrderManagement/OrderBooking/OrderBooking/Id PostOrderByOrderId

MapRoute: MapRoute is the method used to define new routes in our application. The MapRoute method does not belongs to System.Web.Routing assembly but they are the extension methods defined in the System.Web.Mvc assembly.

Ignoring routes : The route with the pattern {resource}.axd/{*pathInfo} is included to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller. 

RouteExistingFiles property : As default the routing infrastructure doesn’t handles requests send for static resources like images, css or js files. This is controlled by the RouteExistingFiles property and the default value is false. There are situations you may have to handle the requests for static resources by actions and that can be done by setting the RouteExistingFiles property to true.

Route constraints : Constraints are used to restrict a matched url to be handled by a route or not.When we passed the route constraint as a string and when we do that it is taken as regular expression. When the passed value matches the regular expression the request succeeds else fails with 404. HttpMethodConstraint is a built-in implementation of IRouteConstraint and it checks whether the request http method satisfies the passed value and if not returns false.

Route handler : Every created route is linked to a route handler. The default route handler that is set for created route is the MvcRouteHandler. A route handler is something that implements IRouteHandler.The MvcRouteHandler instantiates and returns an instance of MvcHandler that handles the request from there.

public interface IRouteHandler
{
    IHttpHandler GetHttpHandler(RequestContext requestContext);
}

2.IRouteHandler

2

It defines a contract that a class must implement in order to process a request for a matching route pattern. It exposes a single method, the GetHttpHandler() method, which is responsible for providing the route handler class instance that will process the request.
MvcRouteHandler Class
It is the responsibility of the route handler to determine the HTTP handler that will serve the request, by looking at the received RequestContext.
The MvcHandler Class
It is the responsibility of MvcHandler class for generating the response for the ongoing request being processed. The MvcHandler class receives information about the ongoing request from the RequestContext passed to its constructor, in the implementation of the GetHttpHandler() method in the MvcRouteHandler class. The MvcHandler class implements three interfaces : IHttpAsyncHandler, IHttpHandler and IRequiresSessionState.IHttpHandler interface defines the contract that a class must implement in order to synchronously process HTTP Web requests using HTTP handler. It exposes a single method, i.e., the ProcessRequest() method.

3.Instantiate & Execute Controller

 ProcessRequest() call to ProcessRequestInit(), which is responsible for extracting controllers name from RouteData collection and request controller factory to create corresponding controller.
3
The ProcessRequestInit(), receives the HttpContextBase object containing Http specific information and references of IController and IControllerFactory type, which will be holding the created copies of controller and controller factory objects at the end.Then, in order to get the controller factory, which is responsible for the creation of instances of any controller class, the ControllerBuilder class is used.After getting the instance of the controller factory, it is then used to instantiate a controller, by making a call to CreateController() method of the DefaultControllerFactory class.
After getting the instance of the controller, the Execute() method is called for this controller. The execute method is responsible for invoking the action method whose name matches the action value in the route data.

Still to come…

Leave a Reply

Your email address will not be published. Required fields are marked *