1.Routing Module
Routing mechanism and MVC5 URL parsing process is basically the same, a lot of interfaces and classes like Iroutehandler, IHttpHandler, IController, RouteBase, RouteTable, The Routedictionary, defaultcontrollerfactory, and arearegistration names are the same features that are similar, or can be seen as a simplified version of the MVC5 routing mechanism or Artech An enhanced version of the routing instance in the MVC5 book. During the start-up phase of a Web project, the set of routing templates (including arearegistration) is registered to Routedictionary, and all types that implement the IController interface in the AppDomain are registered in a thread-safe collection.
Need to focus on the time, combined with the actual experience of the company's existing projects (the company's development norms), the MVC Project Routing template is very formal, can only be this form area/{otherparameter}/{controller}/{action}/{id}, A segment can only be a "/" or a plain text string, or simply a curly brace argument, not a parameter and a text string similar to "/{date}text{time}/" or "/{*parameter}/{ohterparameter}/" And so on will cause complex routing features (and cannot use Routeattribute). There is no need to routeconstraint. So I'm parsing the requested URL for a lot less work.
By traversing the cached route collection, calling the route's match method with request as a parameter, comparing the virtual paths of the Urltemplate and request URLs in each route, in the same text string, The corresponding result of the request URL or the default parameter value in the route is saved to the values dictionary property of the Routedata in the case where the parameter location matches. The defaultcontrollerfactory is called after parsing out the Area,controller and action. The namespace stored in the Datatokens Dictionary of the matching route also has a default namespace configured in the project to find the type that satisfies the namespace and the controller name. instantiated and executed.
2.Action The parameter binding and execution
The action parameter is configured with five sources, namely Routedata,querystring,formvalue,filecollection and a jsonvalue, which, when executing the parameter bindings, Gets the binding of a specific parameter type specified by the user in order of precedence, and calls Defaultmodelbinder as the default binding if not set. Defaultmodelbinder implementation of bindings is still quite complex and difficult to tell, while in the process of binding will also execute Modelvalidator, update modelstate, Overall, Defaultmodelbinder will determine if the action's argument is a simple type or a complex type, and if a complex type uses reflection to parse out all property recursive calls until it is decomposed into all simple types to execute the binding, The properties that are decomposed by all complex types pass in the form of a string expression () to the five data sources to match whether there is a value, and if so, update to Modelstate. In the form of sometimes need to upload files, see the Nancy MVC Framework has a set of file upload mechanism by parsing the header of the boundary token to obtain the file stream to modify some functions, and replace the context and then add to DRISIONMVC.
The real logic of executing the action begins after the binding of the parameter is completed, DRISIONMVC to improve efficiency, All actions are wrapped with the action's MethodInfo as key, and the action's actionmethoddispatcher is the value, Saved to a thread-safe Actionmethoddispatchercache dictionary, the Execute method of Actionmethoddispatcher completes the actual execution of the logic, The action is packaged into a lamdaexpression in the form of Func<object, object[], Task<object>>, and the lamdaexpression is also cached, Gets executed to return the correct result.
3. Compilation Execution of view cshtml files
View view files are compiled with the Razorengine framework, and all. cshtml files are saved to a thread-safe dictionary after they are first compiled into a C # class. The general running flow of the framework is to call the Executeresult method of the Viewresult class when it returns to view () in the Controller's action method, which begins to invoke Razorengine's compile and execute function in the body of the method. The absolute path to the view. cshtml file is obtained according to the Convention, with the absolute path and the type of the view model (null if not present) as the combination key, to find out if there is a compiled view instance in the cache, a direct return to the view instance, the execution view instance returns a full HTML string literal and writes to the RESP The Body property of the Onse. If it does not exist, it will first get the full text of the. cshtml file, while creating a new instance of Typecontext containing the type of the model and the base class of the C # class that was compiled razortemplatebase<> A collection of namespaces is also required. The incoming Roslyn compiler generates a real C # type and saves it. But now there's a problem with applying @model modetype after calling @Html. textboxfor and so on space time without smart hints is also quite a headache, currently temporarily use @inherits to replace.
The development process found that Razorengine did not have @html (HtmlHelper) and @url (urlhelper) helper classes, so the control could not be used when developing Web projects using DRISIONMVC, like @ Html.textbox, @Html dropdowlist, @Html. Action and @url.action. So HtmlHelper is custom in the Razortemplatebase<> base class, there are urlhelper two properties, and the render mechanism of all controls is modified only to use Owincontext as the data source. In addition, the sub-action mechanism and partial is more complex, and spent a lot of energy. View view of the two major control Displayfor,editorfor because the company inside the project is used so not added.
After months of fighting and finally completing the MVC framework, it is comforting to be able to test the application with little or no schema-level errors resulting in some MVC5 common functionality. Feel learned a lot of things, cohesion the idea of the coupling is also quite experienced, but also learned a lot of considerations for the development of the framework.
Three main modules of MVC framework