ASP. NET has no magic-ASP. net mvc Razor and View rendering, mvcrazor

Source: Internet
Author: User

ASP. NET has no magic-ASP. net mvc Razor and View rendering, mvcrazor

For a Web application, its interface is the result that the browser renders and displays to the user based on HTML code and referenced resources, in other words, the Web application interface rendering is completed by the browser. The Web application principle is to obtain the corresponding Html code and related resources from the server through the Http protocol, this allows the browser to complete the correct rendering.

ASP. net mvc, as a Web application construction framework, implements the UI display function. In the development process, View is named by Action. When a user's request is routed to an Action method, ASP. net mvc obtains the corresponding View File Based on the Action name, dynamically processes the View File, generates the final Html content, and returns the content to the browser for display. Therefore, ASP. NET rendering actually refers to the process of dynamically generating Html code.
The action code in ASP. net mvc can be as follows:

   

You only need to call a View method to display the View Index to the user's browser. What does the View method do? What is Razor? What is the returned ActionResult of the Action method?
This article will introduce the process of generating ASP. net mvc Html code from the following aspects:
● ActionResult and ViewResult
● View search and Razor
○ ViewEngineCollection & ViewEngine
○ ViewEngineResult
● View compilation and Activation
● View rendering
● Use the sample code to demonstrate the rendering process of the View
● View Html Helper and ModelMetadata
● Common ActionResult
● Summary

ActionResult and ViewResult

In the previous article ASP. NET has no magic -- ASP. as mentioned in net mvc Filter, the Action method is executed by the ActionInvoker, and the result returned by the Action is of the ActionResult type, after the Action is executed, ActionInvoker calls the ExecuteResult method of ActionResult to complete the specific operation. The relevant code is as follows:

  

  

The ActionResult is defined as follows. It contains a method named ExecuteResult, which is used to process the execution result of the action method:

  

Return to the View () method that was initially mentioned. This method is defined in Controller and its return value is a ViewResult type:

  

It can be said that after the Action is executed, ASP. NET MVCView rendering is completed by ViewResult in the ExecuteResult method., The ExecuteResult implementation code of ViewResult is as follows (Note: This code is implemented in the base class ViewResultBase of ViewResult ):

  

It can be easily seen from the code that the rendering of views in ASP. net mvc involves four steps:
1. If no View name is specified, the View name is Action by default.
2. Search for and obtain the real View object based on the context of the controller.
3. Call the Render method of the View object to write the View content to the response information of HttpContext, and then return it to the browser.
4. Release the View object.

According to the above analysis, the two important steps of View rendering are the View object'sSearchAndRenderingFor details about the entire process, see:

  

View search and Razor

In ASP. net mvc, The View File is generally placed under the Views directory of the Project root directory, with the Controller name as the subdirectory, and each subdirectory stores the View File named after the action method:

  

The following code searches for a View in the ViewResult type:

  

The Code shows that it uses a ViewEngineCollection object to search for a View based on ViewName (actionName by default). If a ViewEngineResult type is returned, an exception is thrown, the exception contains the search location:

  

Note: ASP. NET MVC. cshtml and. in addition to the vbhtml file, it also matches. aspx and. the ascx file is the page file of the Web Form framework. Why? By default, ASP. net mvc contains the Razor engine used by MVC and the Web Form engine used by Web Form. Therefore, when MVC is used only,To optimize performanceThe following code deletes the Web Form engine:

  

More details about the View engine.

ViewEngineCollection & ViewEngine

ASP. NET has an IViewEngine interface, which defines the search and release of views. Its definition is as follows:

  

The type relationships of IViewEngine interfaces implemented in ASP. NET are as follows:

  

The following figure shows the information:
● ASP. NET has two final viewengines: Razor and WebForm. The MVC application uses Razor to render the View.
● Their base classes are VirtualPathProviderViewEngine, which means they manage views based on relative paths.
● Their base classes are both BuildManagerViewEngine, and on the surface they are related to compilation (Note: In ASP. in NET, both WebForm and MVC can write code on the page, which must be compiled before it can work properly ).

ViewEngine in ASP. NET is managed by a set named ViewEngines, for example:

  

In MVC, RazorViewEngine is mainly used and is part of the code of RazorViewEngine:

  

The code shows two important information. The first is"_ ViewStart" is hardcoded as a startup page.This is why the layout is specified on the page.In its construction method, various LocationFormats are hardcoded, And they specify the search path for the corresponding page type..

So what is Razor mentioned above?Razor is a markup language used by ASP. NET to embed server code into a webpage. It consists of Razor, server code (C #/VB), and Html code.. Content starting with the @ symbol in Html will be recognized as server code, and Razor will recognize the code and render it as Html code.

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.