Asp. Net MVC3 introduction to the Asp. Net MVC3 Project (II)

Source: Internet
Author: User

Preface
In the first season of Asp. Net MVC3 (I) Environment preparation, I briefly introduced the installation of Asp. Net MVC3 project and the basic information of the first Asp. Net MVC3 project. I did not detail the functions of various folders in the project and how the first page was created to run? There are still a lot of questions. In this article, we will introduce the roles of various folders in the project in detail, and briefly introduce Asp in the first project. net mvc url-driven.

Section 1: Introduction to Asp. Net MVC3
Let's take a look at the sample of a common Asp. Net MVC3 project, as shown in

 

There is a difference from WebFrom. If you already know Asp. Net MVC2, you will be familiar with it! But there are some differences. In any case, we will introduce them one by one.

The interesting thing is that even if we create an empty MVC project, VS will automatically help us create the directory as shown in. Why? This is because MVC adheres to the idea that "Conventions are greater than configurations" and we are using Asp. net MVC3 development project, you must also pay attention to the work according to its provisions, for example: the Controller needs a View after returning the Action for display (of course, the View () is called () method). Asp. net MVC returns to the Views folder and finds the specific page under the folder with the same Controller name for rendering. Of course, if not, the Shared folder will be searched. The following table shows the roles of folders in Asp. Net MVC3.

Folder Function
 
/Controllers storage controller class [responsibility: process user requests and direct specific pages to the client for rendering]
 
/Views stores the view files corresponding to each controller. If it is a Razor engine, the suffix is cshtml. If the WebFrom view engine is used, the suffix is Aspx.
 
/Content mainly stores photos, CSS, Flash and other files
 
/Scripts mainly stores script files. [Microsoft provides us with the JQuery1.5.1 package by default. It seems that JQuery has become an industry standard! We have no way back. Oh, of course, I personally like JQuery]
 
/Models mainly stores ViewModel classes. [of course, this is not strictly required, but we recommend that you do this .]
 

Other interesting files:

One is Web. Config, and the other is Global. asax. Although we are all very familiar with it, there are many differences with our WebFrom. In the WebConfig file, enable client script verification, configure System. Web. Routing, System. Web. Mvc, and other components. Global. asax registers the Global Area [region, which will be explained later], Global Filter, and routing when the application is started.

Section 2: Request Processing Model of Asp. Net MVC
In the previous article, we also made a simple example to add a Controller directly, and then add a View to the Action to directly run the View, then a common Html page is displayed in front of us. Let's explain in detail this development method or development model. Before explaining this, we should first understand several concepts:

Controller: Controller. The class added to the Contrller folder ending with the Controller is the Controller, and each of its methods is an Action. It is responsible for retrieving data from the Model and handing the data to the View. It is the role of a conductor. It does not control the display logic of the View, but only gives the data of the Model to the View, the specific display of data is the responsibility of the View, so the Controller and View are in a weak coupling state, and the Controller can specify a specific View for rendering. Therefore, the code and entity on the UI Layer are well separated.

View: View. in charge of data presentation, of course, the View code should be written in Html more closely, and the View-layer code writing is directly related to the View engine parsing rules, therefore, the syntax of Razor is quite different from that of the webFrom view engine. I prefer the concise and convenient Razor syntax.

Model: many people regard the Model as a domain Model, while MVC itself is a manifestation Model, which is a framework more inclined to the UI Layer, therefore, the Model we specify is generally used as the ViewModel in use, but the overall MVC idea is that the Model is something related to the field.

After understanding and analyzing the MVC3 modules, we also know some basic concepts of Asp. Net MVC. Next we will analyze a complete Http processing process. See the following figure:

 

The client sends an Http request, which is first captured by our IIS, and then delivered to our Route component according to the Url request format, then it is responsible for parsing the specific Action under the Controller requested by our Url. Then MVC calls our Action for execution after processing. In the Action, we usually retrieve data from the Fa C ade layer of the business, convert the data in the transport layer to the ViewModel, and then deliver the data to the View engine for rendering, finally, the Html byte stream is generated and written back to the client.

In our first project, the Route component parses that the Controller is Home and the Action is Index, creates a Controller instance through the factory, and then calls the InvokeAction method, execute the Index method, and finally execute the View () method to return a ViewResult instance. Then, call your ExcuteResult method to deliver the data context and output stream to the View engine, then the Html page is rendered to the client, and the first page is displayed.

Summary:

Asp.. Net MVC all requests are attributed to the Action, and Asp. net MVC request-processing-the response model is very clear, and without the complicated lifecycle of WebFrom, the entire request processing is very clear and simple, and returns to the original Web development method, is a simple request processing response!

 

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.