The rendering learning of ASP. NET MVC View

Source: Internet
Author: User

to the view of the presentation plate, feel that the study of ASP. NET MVC has come to the end, or more happy, after all, there is no small harvest. This section is relatively straightforward, as it has been specifically learned about how to combine the HTML5 with the MVC framework. As mentioned in the previous article, the implementation of ActionResult is actually the portal of view rendering. Different types of actionresult are more, the following is a brief introduction.

Type

Brief introduction

Emptyresult

The simplest result, just to conform to the framework of the process.

Contentresult

You can set contenttype as JavaScript, CSS, and so on, by default the return value of the base type is converted to Contentresult.

Fileresult

Contains Filecontentresult, Filepathresult, Filestreamresult

Javascriptresult

Generate JavaScript dynamically on the server and as appropriate for the request.

Jsonresult

Provides serialization and serialization of objects and JSON strings

Httpstatuscoderesult

Convenient return HTTP status code

redirectresult

Redirecttorouteresult

Next, the most important viewresult and viewengine concepts are introduced. View implementation iview interface, is actually a render method, parameter ViewContext in addition to the common attributes, there are two JS related attributes (cleintvalidationenabled, unobstrusivejavascriptenabled), specifically the object type of viewbag uses the [Dynamic] attribute. Viewengine also implemented the corresponding interface, note the view cache, the Searchedlocations property contains all the lookup path, the Findview method's return name is rather strange, viewengineresult, But the actual meaning should be that this result contains the view and viewengine information. A further introduction to this piece was made in another tubes definition of Viewengine blog post.

A brief introduction of the next razor engine, we know that the. cshtml file cannot be executed directly, it must be compiled dynamically before it can be used, so the question is, what is the file name compiled into, and what is the assembly that belongs to? And where is it stored?

File name is: asp_page_views_xxx_actionxxx_cshtml

The assembly name is known by the code:

public static mvchtmlstring listviewassemblies (this htmlhelper Helper)

{

tagbuilder ul = tagbuilder (

foreach (var assembly in AppDomain. Currentdomain.getassemblies (). Where (A = A.fullname.startswith ("App_Web_" )))

{

//omit

}

return new mvchtmlstring(ul. ToString ());

}

The path to the compiled temporary file is:

"%windir%\microsoft.net\framework\{versionno}\temporary ASP. Files\"

You can change the path by using the following configuration:

<system.web>

<compilation Tempdirectory=" C:\XXXXXXXX< Span style= "color:black;" > "/>

</system.web>

Add a method that obtains the compiled type and assembly, using the static methods of the Buildermanager type Getcompiledtype and getcompiledassembly.

The type of the view file compilation is Webviewpage<tmodel>, that is,. cshtml the compiled type is the above type. Its base class Webpageexecutingbase contains methods such as Execute, Write, writeliteral, and so on. Another self-ray webpagerendingbase of the base class contains layout, Isajax, Server, Session, Profile, cache properties, and an abstract method executepagehierarchy that outputs the entire page.

Webviewpage also has a direct base class Webpagebase, which implements the class abstraction method Executepagehierarchy, the final rendered page contains 3 parts: The layout file, the Start Page view itself. A good layout example code provided by Mr. Jiang is as follows:

// layout file Definition

<! DOCTYPE html>

<html>

<head>

<title> @viewbag.title</Title>

</head>

<body>

@{if (issectiondefined ("Header"))

{rendersection ("Header");}

Else

{<h2>@viewbag.title</h2>}

@ renderbody ()

@{ if(issectiondefined ("Footer")) {

@ rendersection ("Footer");

}

Else{<hr/><P>@ Xixi. All rights reserved. </P>}

}}

</body>

</html>

//view the definition

<div>

<p>bilibili</P>

</div>

@section Header

{<h2>Bili Bili</h2> }

See this I really understand how the previous renderbody in the code is used, how the layout file and view are combined.

Finally, a picture of the razor engine gives you a clearer impression.

Note: This article is mainly for their own study, the wrong place hope forgive me.

Resources:

[1] Jing Jinnan. The ASP. NET MVC4 framework revealed [M]. Shanghai: Electronic industry Press, 2012. 390-444

The rendering learning of ASP. NET MVC View

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.