ASP. net mvc learning Day 3

Source: Internet
Author: User

Today, let's briefly talk about the view class related to the Razor view engine syntax.

Add an MvcTest project, add a Home controller, and add the view of the index. We will analyze the razor view engine in index. The following is the home controller code.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. web. mvc; 6 7 namespace MvcTest. controllers 8 {9 public class HomeController: Controller10 {11 // 12 // GET:/Home/13 14 public ActionResult Index () 15 {16 return View (); 17} 18 19} 20}View Code

The following is the code for the index view. The comments are all in the code.

1 @ {2 Layout = null; 3} 4 5 <! DOCTYPE html> 6 7 The following is a sonview code used in the index view.

1 @ {2 Layout = null; 3} 4 5 <! DOCTYPE html> 6 7 Below is

The content seems a little messy. Please be patient.

Next, let's look at the assembly path on the computer and decompile it to see the front-end page class.

This is the result of my decompilation. We can see that the front-end page class is compiled into a class named _ Page_Views_Home_Index_cshtml (the name is long. It inherits from the webviewpage class. This class is still generic.

This front-end page class is easy, except for the Execute method. Let's take a look at his parent class, webviewpage class.

Note that there is a Model attribute, and the type of this attribute is TModel, generic. If we write @ model student (data type) in the front-end page class, the front-end page view is called a strong-type view, and a prompt is displayed when Model. Ah, it seems that I forgot to demonstrate it here, so it may be hard for beginners to understand it.

Create a new Student class in the Models folder.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 6 namespace MvcTest. models 7 {8 public class Student 9 {10 public string Id {get; set;} 11 public string Name {get; set;} 12 public string Description {get; set ;} 13} 14}View Code

Add @ model MvcTest. Models. Student to the index view.

When we use @ Model., can we see if there are any prompts for attributes such as name and Id? In this way, a Model-type view is called a strongly-type view. Through decompilation, we can see that the model type is passed through the generic parent class.

One of the several methods for passing data between the Controller and view in the second day is ViewData. In webviewpage <TModel>, we can see that she is a dic type. Next we will look at the webviewpage class,

Here, we can see that the attributes are not viewbag, viewdata, and context. Let's continue to explore the webpagebase class.

However, this class does not seem to have anything we are interested in, so we will continue to look at the webpagerenderingbase class.

There are too many attributes here, such as cache, pagecontext, request, response, server, and session. So far, we should understand why we can access so many attributes in the view.

Next, let's analyze the Controller. We look for the Controller class in The Decompilation tool. Note that this class is in the mvc namespace and is not available in the default decompilation tool, we can find the related dll in the project folder, the path in the project ~ /Packages/Microsoft. aspNet. mvc.4.0.20710.0/lib/net40/System. web. mvc. dll may have different paths because of its version. Put this file in the decompilation tool to find the controller class.

There are too many things in this class. I only cut a part of the graph and we often use it to prove why we can access so many attributes in our controller.

Next we will briefly describe the view loading process. In this way, the _ ViewStart. cshtml file in each folder is first loaded during each view loading process. Some may not understand it. Let's take a look at the views folder in the project.

If you access index. cshtml view, because the view is in the home folder, and the home folder is in the views folder, you need to access index. the cshtml file must go through the views folder and go through the home folder. However, there is a _ ViewStart. cshtml file in the views folder, so you must first load this file. If there is a _ ViewStart. cshmtl file in the home folder, the file will also be loaded. Let's take a look at what is in _ ViewStart. cshtml.

Layout is a Layout variable that will be passed to the view page to be accessed. This is why Layout = null is displayed on each view page. Previously, we assigned a value to Layout and cleared the value after arriving at the specific view. The layout page is not used, which is similar to a template. We know that many websites share the same content, so that we can write the same content to the template, while the specific view can write the specific content, which is easy to use. Fill in the content. You can try it on your own. You can also skip the template page. Let's talk about it today. It's time to have lunch, haha.

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.