MVC basic learning-theoretical and mvc Basic Theories

Source: Internet
Author: User

MVC basic learning-theoretical and mvc Basic Theories
1. Introduction Ø definition? What is MVC?

The full name of MVC is Model View Controller, short for model-view-controller. It is a Model of software design, organize Code by means of separation of business logic, data, and interface display, and integrate the business logic into a component to improve and personalize the custom interface and user interaction, you do not need to rewrite the business logic. MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure.

Ø VC mode and WebForm mode we are familiar

Looking at the two images above, the figure above is the web request method, and the one below is the mvc mode. We can see that, first, the web Client sends a request to the browser, for example, publish. Traditional web development inputs a website and requests a page class.

The second method is the MVC method, which also sends requests to the server, such. Mvc is a method in the request page class, and then transmits the data to the view for display.

The two figures below are clearer:

Traditional web form:


Mvc mode:


2. Internal explanation of the access path of the program

From the above figure, we can see that in MVC, the requested URL of the client is mapped to the corresponding Controller, and then the Controller processes the business logic, you may need to retrieve data from the Model and then select an appropriate View from the Controller and return it to the client.

Create a small program:Create a solution and add a controller. The Code is as follows:

<Span style = "font-family: Microsoft YaHei; font-size: 14px;"> <strong> using MVCBlog. models; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; namespace MVCBlog. controllers {// 1. controller class (inherited from Controller) public class HomeController: Controller {// create a data set (pseudo data) # region 0.1 initialize data set + void InitData () /// <summary> /// initialize the data set /// </summary> public List <Models. do G> InitData () {List <Models. Dog> list = new List <Models. Dog> () {new Dog () {ID = 1, Name = "sample ~~ "}, New Dog () {ID = 2, Name =" sample 2 ~~ "}, New Dog () {ID = 3, Name =" sample 3 ~~ "}, New Dog () {ID = 4, Name =" Sample 4 ~~ "}}; Return list ;}# endregion // 2. the Action method (can be seen as the Model of the MVC design pattern) public ActionResult Index () {// defines a container and designs the capacity System. text. stringBuilder sbHtml = new System. text. stringBuilder (4000); // 2.1 reads the current business, such as database reading and judgment) // 2.1 creates a data set to obtain the current data List <Models. dog> list = InitData (); // 2.1.1 traverses the set, generates html code, and saves it to the sbHtml list. forEach (d => {sbHtml. appendLine ("<div>" + d. toString () + "</div>") ;}); // use viewBag to pass data to the Index with the same name. cshtml // viewBag is a set of dynamic types. You can dynamically add attributes and values of any type of name to ViewBag. htmlstr = sbHtml. toString (); // 2.3 load the View Index with the same name. cshtml return View () ;}}</strong> </span>

Then add a dog class in Models. The Code is as follows:

<span style="font-family:Microsoft YaHei;font-size:14px;"><strong>using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MVCBlog.Models{    public class Dog    {        public int ID { get;set; }        public string Name { get; set; }        public override string ToString() {            return "ID=" + this.ID + ",Name=" + this.Name;        }    }}</strong></span>

Right-click the index method to add a view. The view code is as follows:


<span style="font-family:Microsoft YaHei;font-size:14px;"><strong>@{    Layout = null;}<!DOCTYPE html>

Run it directly:The effect is as follows:


We can see that no matter whether you access the former address or the latter address, the content is the same, indicating that the access is http: // localhost: 9711/Home/index by default.


We noticed that the URL in the address bar is Home/Index. If we follow the WebForm mode described above, we should be able to find the Home directory under the root directory of our project, then there is an Index file in the Home directory, but we cannot find the Home directory in the root directory. However, let's find the Views/Home/Index. aspx file in the Views directory. Let's enter this address to run it:


Why? The path is correct but cannot be displayed. Why? Let's analyze it.

Why does the input path of the MVC operating mechanism seem correct but incorrect?

We can see that even if the input path is correct, the error still persists. What is the problem?

Open the configuration file of the Views folder. In fact, there is a configuration file under this folder,


This solves the problem of the 404 error when we directly access the files in Views/Home/Index. aspx? In MVC, it is not recommended to directly access the View, so when we access, that is, all the files under the Views directory will be accessed by the System. web. httpNotFoundHandler so we do not place resource files (CSS, JS, images, etc.) in the Views directory.

Why do the first two inputs have the final result?

In MVC, the requested URL of the client is mapped to the corresponding Controller. Then, the Controller processes the business logic and may retrieve data from the Model, then, the Controller selects the appropriate View and returns it to the client.

When the ASP. net mvc program we run accesses this URL, it actually accesses the Index Action in HomeController.


So why is the first address accessible? http: // localhost: 9711, because of the MVC routing mechanism. Let's look at the routing configuration:


We can see that a Route named "Default" is defined here, and the Default parameter is also defined. The default parameter indicates that when we access a URL such as http: // localhost: 9711, it will add nonexistent parameters with the default parameter, which is equivalent to accessing http: // localhost: // localhost: 9711/Home/IndexSame. Therefore, the above two results are the same.

MVC Core Components

In the MVC design Model, the Model refers to the business logic and data operations to be processed; the View mainly refers to dealing with users and displaying them to users; controller is a bridge between Model and View.

The first Controller is the Controller.

Many people are asking where the Model is. Is it in the created Models folder? In fact, the/Home/action method can be regarded as the Model of the MVC design pattern, this method performs operations based on the classes or data created in Models.

The second is Models.

If you have learned three layers, you may not be familiar with them. The entity in the three layers is like this. Are they the same? There are similarities and differences. We can not only define entities, but also establish data models, that is, EF (EntityFramework ); that is, ORM (Object relationship ing framework/data persistence framework) is an object-oriented operation framework for operating data in a data table based on an object. The underlying layer also calls ADO. net.

The third is Views.

After the request sent by the client browser is processed by the method index in the Controller, the Controller selects the appropriate View and returns it to the client. It is mainly presented to users.

3. Summary

Here is just a preliminary understanding. Here we mainly talk about the theory. The next article will explain some applications and other knowledge. Continue to follow up ~



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.