MVC Tutorial One: Introduction to MVC

Source: Internet
Author: User

I. Overview

1, MVC is a popular Web application architecture technology, he divides the Web application into models (model), controllers (Controller) and view (view) three parts.

2, Advantages: Can be tested, can be maintained

3. MVC is a child (ASP) of ASP. NET includes: ASP. WebForm and ASP.

Ii. Introduction to MVC

1. What is MVC?

MVC is a software architecture design pattern for software and programs that have a human-computer interaction function.

2. Order of execution

The controller receives a request from the Web page, and if it needs to request data, first extract the data from the model and then hand it over to the view display.

3, the nature of web development

The essential process of web development: The response of request----processing, the webform approach is not close to the nature of web development.

Iii. creating an ASP. NET MVC Step

1. Create an ASP. NET MVC project.

FILE--New Project->web project, select the appropriate version of the ASP. NET MVC application as needed, and click "OK":

2. In the project template selection interface, select the appropriate template and view engine, click OK, create a new ASP. NET MVC application:

3. Create controller controllers.

This example takes VS2012 as the development tool to create a MVC4 application that requires the creation of a controller to run (VS2013 or later has already created the controller by default and can run directly).

On the Controllers folder, right-click Add Controller:

Enter the name of the controller (the controller's name convention ends with the controllers), select the template, and click Add:

4. Create a view.

In the Controller code interface, right-click on the method name: Select "Add View":

In the Add View tab interface, enter the view name, the view name must be consistent with the name of the method, select the view engine, click Add:

In the Views folder you will see the newly added view:

Iv. ASP. NET MVC Project Structure

The newly created MVC application structure:

1. Content Folder

Store resource files, such as CSS files, third-party plugins, and so on.

2. Controllers folder

Store the controller files associated with the MVC application.

3. Models folder

Files other than controller and view are stored.

Note: Unlike the models of a three-tier structure , the models inside MVC can contain entity classes, data access classes, business classes, and so on.

4. Scripts folder

Store various script files.

5. Views folder

Store the view file.

6. Shared Folder

The shared folder is located in the Views folder, and any view not found in the controller will go to the shared folder.

7. Web. config

Store configuration information.

8, Global.asax

Global.asax store the code to execute when the application starts.

Five, the code detailed

1.global.asax-application initiates registered routing

When a request is made to a website, how the application leads the request to the corresponding controller and the action method inside it
A: First when the site starts, the routing rules set according to the controller and the action method are loaded ahead of time, so that the route is analyzed and the request is routed to the corresponding controller and action method.

The Routeconfig class is located under the App_start File folder.

2. Controller

1) According to the previous resolution, each request is the first to locate the controller, and then according to the routing rules to find the corresponding action Method!
2) But in the routing table, we only wrote home, but the MVC framework agreed that the controller ends with the controllers and inherits the base class controller!
3) Here the Index method return value type is ActionResult, method name and route registration method name is the same, within the index method, call the View method and return, this is an overloaded method, you can give a parameter, as the name of the return view, if there are no parameters, The default is a view named after the current action.
4) that is the ASP.NETMVC architecture, unlike the Java MVC configuration to establish a relationship, he is the contract is greater than the configuration.

3. View

1) The code in the view is similar to the previous ASPX code, mainly HTML, JS and CSS, such as front-end technology, from MVC1.0 to MVC5.0 great changes, razor syntax more and more perfect, powerful!

4. Passing data to the view

1) The traditional WebForm way, the predecessor page class and the Post code class are the inheritance relations, can carry on the data transfer directly.
2) in MVC mode, views and controllers are completely detached, passing data through a dedicated channel, ViewData is one of the containers that pass data between the controller and the view.

5. asp. NET MVC execution sequence

Vi. model and three-tier architecture

1. Is there a difference between MVC and the three-tier architecture?
1) There is no inherent relationship between MVC and the three-tier architecture, and the idea of architecture is not the same.
2) The three-tier architecture emphasizes physical separation (multiple project components), and MVC emphasizes logical separation (a project component).
3) The three-tier architecture can also be embodied in MVC.

4) Some MVC Enterprise framework is deliberately designed to be models for ease of management.

Note: in in MVC , if it's not part of the View, it's not a Controller. Model the.

VII. MVC and ASP.

1. asp. NET MVC and ASP.

(1) ASP. NET MVC is a subset of ASP.
(2) ASP. NET MVC is built on top of core ASP.
1. Dependent on HttpHandler, such as how the request enters the controller
2. Rely on the session, Cookie, Cache, applaction and other state retention mechanism
3. Use HttpContext, Request, Response, server, and other objects
4. These objects are easily available to controllers using IntelliSense
(3) ASP. NET MVC is an optional way to write web programs
1.WebForms technology is another way to choose

2. Mvc and WebForms

(1) Features of WebForms
1) What you see is what you get, the development of Fools
1. Server-side controls
2. Event Model
3. State management
2) Learn from WinForms's success features
3) Deviate from the principle of Web request processing
(2) Features of ASP. NET MVC
1) Focus on separation
1.WebForm just divided a page into the front page and the post code, not thorough
2.MVC can divide a page into three pieces and divide it more thoroughly.
2) testability
3) Development closer to the nature of the Web
4) Higher technical requirements for developers

MVC Tutorial One: Introduction to MVC

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.