asp.net mvc razor Engine: Razorview

Source: Internet
Author: User
Tags abstract definition constructor

The razor engine has two core types, one representing the view itself type Razorview, the other is the razorviewengine that gets and creates it, and we'll dissect them separately with two articles. The view under the Razor engine is represented by the type Razorview, which is a buildmanagercompiledview subclass of the type Webformview that represents the view of the Web Form engine.

First, Buildmanagercompiledview

In order to be able to articulate the view activation and rendering mechanism in Buildmanagercompiledview, we list the internal and protected members in the Buildmanagercompiledview that are associated with this.

 1:public abstract class Buildmanagercompiledview:iview 
2: {
3:internal iviewpageactivator Vie Wpageactivator;
4:
5:protected buildmanagercompiledview (controllercontext controllercontext, String viewPath);
6:protected Buildmanagercompiledview (ControllerContext controllercontext, String viewPath, Iviewpageactivator viewPa Geactivator);
7:internal Buildmanagercompiledview (controllercontext controllercontext, String viewPath, Iviewpageactivator V Iewpageactivator, Idependencyresolver dependencyresolver);
8:
9:public void Render (ViewContext viewcontext, TextWriter writer);
10:protected abstract void Renderview (ViewContext viewcontext, TextWriter writer, object instance);
One:
12:internal ibuildmanager BuildManager {get; set;}
13:public string ViewPath {get; protected set;}
:}

Through the introduction of the view compiler principle, we know that the view file (. cshtml or. vbhtml) using the Razor engine will eventually be compiled into a webviewpage type, so the razorview/ The presentation mechanism of Webformview embodied view is ultimately embodied in the activation of the Webviewpage object. We can use BuildManager to get the compiled type based on the virtual path of the view file. From the name can also be seen, Buildmanagercompiledview internal is the use of BuildManager based on the specified view file virtual path to complete the Webviewpage object activation.

The Buildmanagercompiledview property Viewpath represents the virtual path of the view file, which is initialized in the constructor. Buildmanagercompiledview has three constructors, and the construction logic of the object itself is embodied in the internal constructor. As shown in the code snippet above, the constructor has an additional two arguments in addition to the current ControllerContext and view file virtual path as a constructor parameter. The types are iviewpageactivator and idependencyresolver respectively.

   1:public Interface Iviewpageactivator
2: {
3: object Create (controllercontext controllercontext, type type);
4:}

The code snippet above embodies the definition of the interface iviewpageactivator. As the name suggests, the interface is designed to enable activation of the Webviewpage object, and the type based object activation mechanism is implemented in the Create method. The viewpageactivator specified in the Buildmanagercompiledview constructor is used to initialize the inner field viewpageactivator, If you do not explicitly specify a Viewpageactivator object through a constructor, the default is to use a Defaultviewpageactivator object.

Defaultviewpageactivator is an internal type with the following definition, and we can see that it actually relies on a Dependencyresolver object to complete the activation of the Webviewpage object. This Dependencyresolver object can be explicitly set through the constructor, and the default Dependencyresolver object is derived from the static property current of the Dependencyresolver type.

1:internal class Defaultviewpageactivator:iviewpageactivator
2: {
3:private func<idependencyresolver> _resolverthunk;
4:public Defaultviewpageactivator (): This (null)
5: {}
6:
7:public defaultviewpageactivator (idependencyresolver resolver)
8: {
9:func<idependencyresolver> Func = null;
10:if (resolver = = null)
11: {
12:this._resolverthunk = () => dependencyresolver.current;
13:}
14:else
15: {
16:if (func = = null)
17: {
18:func = () => resolver;
19:}
20:this._resolverthunk = func;
21:}
22:}
23:
24:public Object Create (controllercontext controllercontext, type type)
25: {
26:return (This._resolverthunk (). GetService (type)?? Activator.CreateInstance (type));
27:}
28:}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.