[Dot Net Core](Graphic series )7. Http Request to Dot Net Core MVC

Czxdas
3 min readJan 5, 2022

--

In the previous section, we described how Middleware works in .Net Core. This section will look at how to send the Http Request to the MVC of .Net Core to generate the Controller.

The following figure shows how it works when sending a Request to .Net Core via URL and MVC is required:

At the top left, it is sent to Dot Net Core Host through the URL with MVC Controller and Action. The middleware it will execute will be EndpointMiddleware.Invoke. After translation through the Routing component, an HttpContext object will be generated, which is called DefaultHttpContext here. In EndpointMiddleware.Invoke, it will find the delegate function of ActionEnpointFactory generated by CreateRequestDelegate, and then execute the extension method GetEndpoint of DefaultHttpContext to generate RouteData, Endpoint, and ActionDescriptor objects, which are assigned to the RouteData, HttpContext and ActionDescriptor properties of the ActionContext respectively.

After ActionContext collects Request information and prepares related objects, it will be sent to the ActionInvokerFactory.CreateInvoker function.

The ActionContext is passed to the ActionInvokerFactory.CreateInvoker function, and then assigned to the ActionInvokerProviderContext.ActionContext. Then it is transferred to ControllerActionInvokerProvider.OnProvidersExecuting to further generate ControllerContext entity, and let the inherited parent class completely copy ActionInvokerProviderContext.ActionContext.

Continue to find ControllerActionInvokerProvider._controllerActionInvokerCache, which is ControllerActionInvokerCache, and execute GetCachedResult.

The GetCachedResult function will find the static class FilterFactory, which has several FilterItems: UnsupportedContentTypeFilter, ClientErrorResultFilter, ModelStateInvalidFilter, ApiControllerAttribute.

These FilterItems will be wrapped in FilterProviderContext.Results, FilterProviderContext.ActionContext is also assigned ControllerContext, the entire FilterProviderContext instance will be passed to ControllerActionInvokerCache._filterProviders (ie DefaultFilterProvider) OnProvidersExcuted and OnProvidersExcuting execution.

An important mention here is that ControllerFactoryProvider is an instance that was previously released by DI. It is a singleten and is assigned to ControllerActionInvokerCache._ControllerFactoryProvider.

Finally, ControllerActionInvokerCache._currentCache is converted to ControllerActionInvokerCacheEntry, and the newly added ControllerActionInvokerCacheEntry object and FilterItem are returned.

After ControllerActionInvokerCache returns the newly added ControllerActionInvokerCacheEntry object and FilterItem collection object, the ControllerActionInvoker object is added successively, and ControllerContext is assigned to ControllerActionInvoker._controllerContext and ControllerActionInvoker._actionContext. ControllerActionInvokerCacheEntry is assigned to ControllerActionInvoker._cacheEntry, and FilterItem collection objects are assigned to ControllerActionInvoker._filters.

The following figure shows the successive execution of ControllerActionInvoker.InvokeAsync:

According to steps S1 to S4 in the above figure, the ControllerFactory of ControllerActionInvoker._cacheEntry will be executed, which is actually the ControllerFactory of ControllerActionInvokerCacheEntry. It will execute the delegation function generated by the ControllerFactoryProvider through its CreateControllerFactory, obtain the controller Type through the ControllerContext object parameter, and let ServiceProviderEngineScope.GetService. Get examples. ServiceProviderEngineScope.GetService can refer to “[Dot Net Core Diagram] 3. How to actually implement DI Resolve Service”.

After obtaining the Controller instance, it will continue to call the action function from the information of the Action, and finally generate the ActionResult. There will be a chance to detail its process later.

The above is an overview of the corresponding controller instance generated after the Request sent by Http to the Dot Net Core Host, for the reference of the overall process.

--

--

Czxdas
Czxdas

Written by Czxdas

Keep looking for Prajna wisdom

No responses yet