ASP. NET MVC view (two)

Source: Internet
Author: User

ASP . NET MVC View ( two ) Preface

For the view engine in the previous article, just a simple demonstration, the work of the real understanding of the view engine may be a little vague, this article will be provided by the MVC framework to our razor view engine of the entire execution process to do a rough explanation, The goal is to make an impression on the execution of the Razor view engine so that Lenovo can think about the role of the view engine and how the view is represented in the MVC framework.

ASP . NET MVC View
    • Custom View Engine
    • Razor View Engine Execution procedure
    • Razor Dependency Injection, Custom view helper for views
    • segmentation, use of partial views
    • Razor syntax, view helper

Razor view Engine Execution process

We have seen the last page of the attempt to run the engine's implementation of a general understanding, and the Razor view engine execution is more detailed, not as rough as the previous article, take a look, figure 1.

Figure 1

Figure 1 shows an inheritance relationship of the object structure in the Razor view engine, and for the right part of Figure 1 it means that the view engine is viewenginesfrom the system. Engines is the viewenginecollection collection type, and in the viewenginecollection The following section represents an inheritance relationship of the razor attempt engine.

Figure 2

Let's take a look at the process of the red pointer part:

When we use ViewResult as the return type of the Controller method, the MVC framework first executes the Findview () method in ViewResult, while the Findview () in ViewResult Does not perform the task of finding views by itself, the MVC framework is a lot of nosy and it interferes. It will be viewenginesfrom the system. Engines is the viewenginecollection collection type that razor the task of trying the engine to perform a lookup view, "Figure 1 cites the inheritance structure of the Razor view engine."

The task that is followed by the virtualpathproviderviewengine type to perform the lookup view, Actually here according to the view name lookup is not our definition of the xxxx.cshtml view file, but the xxxx.cshtml file compiled by the CS file, these CS files represent the corresponding view of the code, which is explained at the end of this article, then these CS files are generated? When the request arrives at the controller, the good MVC framework will take the view folder or the corresponding area of the view folder, all the views on the Controller folder will be compiled into CS file.

Pull the ball away. Back to the topic, after the virtualpathproviderviewengine type finds the view "CS file", it will let the razorviewengine type to generate the view processing class, that is, the implementation of the The razorview type of the IView is typed and encapsulated into the Viewengineresult type instance.

Let's explain the process of the blue pointer part:

At this point the MVC framework invokes the render () method in the View property of the Viewengineresult type, which is actually the method in the Razorview type instance just described above.

Then we look at Figure 2, in the execution of the render () method, the first is to read the file by the buildmanagerwrapper type according to the path of the view and dynamically compile it back to the type represented in the view CS file, where The Buildmanagerwrapper type is the internal type of the MVC framework that implements the internal Ibuildmanager interface,buildmanagerwrapper The meaning of the type is that for a package of dynamic compilation functions, it is actually called System.Web.Compilation.BuildManager function.

Back to the topic, the type was generated after the Defaultviewpageactivator type to generate the C # type "system.web.mvc.webviewpage<dynamic>" corresponding to the view, and finally by the Renderview () method in the Razorview type makes a fundamental assignment to the C # type of the view that we have just generated, such as getting viewdata from the viewcontext type parameter, ViewBag and so on data information "ViewContext type inherits from ControllerContext, so you know" to assign value.

Finally, the webpagerenderingbase type of object to do rendering processing, this part of the content will not be elaborated.

The above is the multi-razor view engine is the Viewresult type of processing process, it is very rough, we would like to be able to understand the view of everyone to help.

The following is a look at the CS file compiled by the MVC framework for the view file.

First, let's look at the results of the operation of a view, Figure 3.

Figure 3

The corresponding view Code "code in the cshtml file", such as code 1-1.

Code 1-1

@{    viewbag.title = "Index";} < H2 > Index</h2>@foreach (var item in Model) {   <H3 > ID: @item. ID Name: @item. Name</h3>}

So, after the request reaches the controller, the compiled view CS file exists?

In the system C:\Users\ the user name of your system \appdata\local\temp\temporary ASP. Net files, of course, not under this folder, but in here will generate some other messy name of the folder, find one.

I was looking for C:\Users\ your system's user name \appdata\local\temp\temporary the ASP. NET files\root\19537580\dfb4a136 folder, Of course. The naming of CS files is not the same as the name of the view, which is usually named after the App_web. After some search found the corresponding code 1-1 CS file, take a look, code 1-2

Code 1-2

#pragmaChecksum "E:\JY\JY\Action\ASP.NET mvc\systemcase\mymvcapplication\mvcapplication\views\iocdemo\index.cshtml" "{ 406ea660-64cf-4c82-b6f0-42d48172a799} "" 11002ef3288cead21a96ac68fd35c045 "//------------------------------------------------------------------------------//<auto-generated>//This code is generated by the tool. //runtime version: 4.0.30319.1008////changes to this file may result in incorrect behavior, and if//regenerate the code, and the changes will be lost. //</auto-generated>//------------------------------------------------------------------------------namespaceASP {usingSystem; usingSystem.Collections.Generic; usingSystem.IO; usingSystem.Linq; usingSystem.Net; usingsystem.web; usingSystem.Web.Helpers; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.WebPages; usingSYSTEM.WEB.MVC; usingSystem.Web.Mvc.Ajax; usingSystem.Web.Mvc.Html; usingSystem.Web.Routing;  Public class_page_views_iocdemo_index_cshtml:system.web.mvc.webviewpage<dynamic> {        #lineHidden Public_page_views_iocdemo_index_cshtml () {}protectedasp.global_asax applicationinstance {Get {                return( (Asp.global_asax) (context.applicationinstance)); }        }                 Public Override voidExecute () {#line1 "E:\JY\JY\Action\ASP.NET mvc\systemcase\mymvcapplication\mvcapplication\views\iocdemo\index.cshtml"Viewbag.title="Index"; #lineDefault#lineHiddenWriteliteral ("\r\n"); #line6 "E:\JY\JY\Action\ASP.NET mvc\systemcase\mymvcapplication\mvcapplication\views\iocdemo\index.cshtml"foreach(varIteminchModel) {                        #lineDefault#lineHiddenWriteliteral (""); #line8 "E:\JY\JY\Action\ASP.NET mvc\systemcase\mymvcapplication\mvcapplication\views\iocdemo\index.cshtml"Write (item.id); #lineDefault#lineHiddenWriteliteral ("Name:"); #line8 "E:\JY\JY\Action\ASP.NET mvc\systemcase\mymvcapplication\mvcapplication\views\iocdemo\index.cshtml"Write (item.                        Name); #lineDefault#lineHiddenWriteliteral (""); #line9 "E:\JY\JY\Action\ASP.NET mvc\systemcase\mymvcapplication\mvcapplication\views\iocdemo\index.cshtml"}                        #lineDefault#lineHidden        }    }}

It must be clear to everyone here, for view files cshtml, Vbhtml and so on. Finally, the type System.Web.Mvc.WebViewPag is compiled at run time, as for the generic type after the type is the dynamic type corresponds to the normal view, and the type of the strongly typed view will replace the dynamic type here with the strongly typed view ViewModel Type, and finally said to #line the meaning of the Niang, is easy for us to debug.

Jinyuan

Source: http://www.cnblogs.com/jin-yuan/

This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.