ASP. net mvc view (2), asp. netmvc View

Source: Internet
Author: User
Tags visual studio 2010

ASP. net mvc view (2), asp. netmvc View
ASP. NET MVCView(II)Preface

In the previous article, the view engine is just a simple demonstration. It may be a bit fuzzy to really understand the work process of the view engine, this article will give a rough explanation of the entire execution process of the Razor view engine provided to us by the MVC framework, the purpose is to impress you on the execution process of the Razor view engine so that you can think about the role of the view engine and the representation of the view in the MVC framework.

 

ASP. NET MVC View
  • Custom view Engine
  • RazorView engine Execution Process
  • RazorView dependency injection and custom view guides
  • Use of segment and segment views
  • RazorSyntax and view guides

 

Razor view engine Execution Process

You have read the previous article to have a rough understanding of the execution process of the engine, while the execution of the Razor view engine is more detailed and will not be as rough as the previous article. Let's take a look, Figure 1.

Figure 1

Figure 1 shows the inheritance relationship of the object structure in the Razor view engine. For the right part of Figure 1, it means that the view engine isViewEngines. Engines, that isViewEngineCollectionInViewEngineCollectionThe following section indicates an inheritance relationship of the Razor engine.

Figure 2

Let's first look at the process of the red pointer:

When we useViewResultWhen the return type of the Controller method is used, the MVC framework first executes the FindView () method in ViewResult.ViewResultIn FindView (), it is not for itself to execute the task of searching the view. The MVC framework is very nosy and will interfere with it. It willViewEngines. Engines, that isViewEngineCollectionThe Razor engine is used to search for a view from the set type. [Figure 1 illustrates the inheritance structure of the Razor view engine ].

ThenVirtualPathProviderViewEngineType to execute the query view task. In fact, the query by view name is not our defined xxxx. cshtml View File, instead of xxxx. the cs files compiled by the cshtml file indicate the code of the corresponding view in these cs files. This will be explained at the end of this Article. When will these cs files be generated? When the request arrives at the controller, the good MVC Framework will put the View folder or the View folder in the corresponding area, all views in this controller folder are compiled and generated as cs files.

Go back to the topicVirtualPathProviderViewEngineAfter the view type is found, [cs file] will makeRazorViewEngineType to generate a view processing class, that is, to achieveIViewOfRazorViewType and encapsulatedViewEngineResultType instance.

The following describes the process of the blue pointer:

At this time, the MVC Framework will callViewEngineResultThe Render () method in the View attribute of the type, which is actually described aboveRazorViewMethod in the instance type.

Then we can see Figure 2. during the execution of the Render () methodBuildManagerWrapperThe Type reads the file according to the view path and returns the type in the cs file of the view after dynamic compilation.BuildManagerWrapperThe type is the internal type of the MVC framework.IBuildManagerInterface,BuildManagerWrapperThe type indicates an encapsulation of the dynamic Compilation function. In fact, the function in System. Web. Compilation. BuildManager is called.

Return to the topic. After the type is generated, the C # type [System. Web. Mvc. WebViewPage <dynamic>] corresponding to the view is generated by the DefaultViewPageActivator type.RazorViewThe RenderView () method in the type to assign a basic value to the C # type of the view we just generated, for example, fromViewContextObtain ViewData, ViewBag, and other data information in the type parameter 【ViewContextType inherited fromControllerContext, So you know.

Will beWebPageRenderingBaseThis part of the content is not described.

The above is the process of multi-Razor view engine, that is, the ViewResult type. It is rough to say. Sorry, we hope this will help you understand the view.

Next, let's take a look at the cs files compiled by the MVC Framework for the view files.

First, let's take a look at the running results of a view, as shown in figure 3.

Figure 3

The corresponding view code [code in the cshtml file], such as code 1-1.

Code 1-1

@{    ViewBag.Title = "Index";}

So after the request reaches the controller, where does the view cs file after compilation exist?

In the system C: \ Users \ User Name of your system \ AppData \ Local \ Temp \ Temporary ASP. NET Files, of course, is not in this folder, but will generate some other folders with messy names here, just find one.

In C: \ Users \ your system USERNAME \ AppData \ Local \ Temp \ Temporary ASP. NET Files \ root \ 19537580 \ dfb4a136 folder, of course, the cs file name does not correspond to the view name, usually started with App_Web. After some searches, we found the cs file corresponding to code 1-1. Let's take a look at code 1-2.

Code 1-2

# Pragma checksum "E: \ JY \ Action \ ASP. net mvc \ SystemCase \ MyMvcApplication \ MvcApplication \ Views \ iocdemo \ Index. cshtml "" {406ea660-64cf-4c82-b6f0-42d48172a799} "" 11002EF3288CEAD21A96AC68FD35C045 "// outputs // <auto-generated> // This code is generated by the tool. // Runtime version: 4.0.30319.1008 /// changes to this file may lead to incorrect behavior. If // re-generates code, these changes will be lost. // </Auto-generated> // -------------------------------------------------------------------------------- namespace ASP {using System; using System. collections. generic; using System. IO; using System. linq; using System. net; using System. web; using System. web. helpers; using System. web. security; using System. web. UI; using System. web. webPages; using System. web. mvc; using System. web. mvc. ajax; using System. web. mvc. html; using System. web. routing; public class _ Page_Views_iocdemo_Index_cshtml: System. web. mvc. webViewPage <dynamic >{# line hidden public _ Page_Views_iocdemo_Index_cshtml () {} protected ASP. global_asax ApplicationInstance {get {return (ASP. global_asax) (Context. applicationInstance);} public override void Execute () {# line 1 "E: \ JY \ Action \ ASP. net mvc \ SystemCase \ MyMvcApplication \ MvcApplication \ Views \ iocdemo \ Index. cshtml "ViewBag. title = "Index"; # line default # line hiddenWriteLiteral ("\ r \ n 

We can see it clearly here. For the type of view files cshtml, vbhtml, and so on, finally compiled into System at runtime. web. mvc. webViewPag. The generic type following the type is dynamic type, which corresponds to a common view, the compiled type of the strong type view will replace the dynamic type here with the ViewModel type of the strong type view. Finally, let's talk about the meaning of # line, it is convenient for debugging.

 

Author: Jin Yuan

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

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.


How does aspnet display data in mvc2 mode?

Visual studio 2010 + sql2008 can be implemented in a few simple steps:
1. Add LINQ to SQL in Models
2. View and server resource manager: Drag tables directly into the design view.
3. Add Controllers to Controllers and select Create, Update... to add operation methods.
4. Introduce namespaces and instance classes
5. public ActionResult Index ()
{
List <Table Name> model = Instance name. Table name. ToList ();
Return View (model );
}
6. Add a view, create a strong view, and select a view data class. OK!

How does aspnet mvc display data in the view?

Check whether you are using MVC2 or MVC3, and whether you are using the aspx or Razor engine.
The Razor engine is used in MVC3 as follows:

Controller:

// If there is paging, the page must be submitted by form; otherwise, no parameters are required.
Using entity class;
Using data warehouse Interface Class; // operation Database

// Class...
IRepository _ ir; // repository object
Public Constructor (IRepository ir)
{
_ Ir = ir; // low coupling Incoupled
}
Public actionresult Index (int? Pageindex, formcollection form ){
Entity1 st1 = new Entity1 ();
Entity. Field 1 = "xxx"; // set conditions for the query
St1 = _ ir. FindData (st1); // query data
ViewData = st1;
Return View ();
}

View:
@ Model... bind an object
@ M bind pagination...
//... Set the template page
@{
Layout = null; // do not use the master page
}
<Form>
@ Html. textboxfor (m => m. Field 1, null) // lamoda binds Field 1
// Bind N data entries to the same table
</Form>
// Display the page number of a total of data entries by page. DWZ is used here.

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.