Web development-MVC Framework and webmvc framework

Source: Internet
Author: User
Tags zend framework

Web development-MVC Framework and webmvc framework

What is the MVC framework?


Here, I would like to briefly describe that if you want to learn more about MVC, you can go to Baidu encyclopedia, or find relevant books to study and understand them carefully, but one thing to note is, it is not easy to fully understand MVC.


The Model View Controller (MVC) is short for Model-View-Controller. It is a software framework Model that decouples the input, processing, and output of an application, separate the implementation code of M and V so that the same program can use different forms of representation.


Simply put, the content of the three core components of the MVC framework is Model, View, and Controller. They process their own tasks, so that different developers can develop views, controller logic, and business logic at the same time.

 

Model: indicates the Business Code and data operation code to be processed.

View: views are mainly used to interact with users and display data.

The Controller serves as a bridge between Model and View. It accepts user input and calls models and views to fulfill user requirements.

 

How to Use MVC programming?


Since MVC is a general framework, we can use VS to create a solution using the MVC framework, and then write our own code to implement the required functions.


First, open VS and create a project, as shown in:


Click OK and you will see that the MVC framework has been automatically created for us in Solution Explorer on the right, as shown in:


Then we can create a model, Controller, and view under the corresponding file.

Let's look at a small example. Right-click the Controllers folder to add a controller, as shown in:



Then add an object class to the Models file. The Code is as follows:

<span style="font-size:18px;">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;        }    }}</span>

In this way, we can write code in the controller. The Code is as follows:

<Span style = "font-size: 18px;"> namespace MVCBlog. controllers {public class HomeController: Controller {// GET:/Home/public List <Models. dog> InitData () {List <Models. dog> list = null; list = new List <Models. dog> () {new Dog () {ID = 1, Name = "Tibetan mastiff"}, new Dog () {ID = 2, Name = "sheepdog "}, new Dog () {ID = 3, Name = "GIWA"}, new Dog () {ID = 4, Name = "husky" }}; return list ;} public ActionResult Index2 () {System. text. stringBuilder sbHtml = new System. text. stringBuilder (4000); List <Models. dog> list = InitData (); list. forEach (d => {sbHtml. appendLine ("<div>" + d. toString () + "<div>") ;}); ViewBag. htmlStr = sbHtml. toString (); return View () ;}}</span>

Finally, create a view by right-clicking the Index2 method in the Controller and selecting add view, as shown in:


We can see that there is a file named Index2.cshtml in the Home folder under the Views folder. This is the view file. We can write a line of code in the view to call the method in the controller, the Code is as follows:

<span style="font-size:18px;">@{    Layout = null;}<!DOCTYPE html>

The following describes how to run a program and run a traditional Web application. In the address bar of a browser, we enter http: // localhost: 60063/Views/Home/Index2.aspx, this is not the case for applications written using MVC. You only need to call the corresponding view and enter http: // localhost: 60063/Home/Index2 in the browser, the result is as follows:


Summary: for MVC frameworks, many people will think of layer-3 frameworks. They do have similarities, all of which are stratified decoupling of applications, but in comparison, the three components of the MVC Framework are completely decoupled, and they are independent of each other. Changing one of them will not affect the other two, achieving low coupling, the biggest benefit of this is the high maintainability. MVC also has other advantages. Of course, it also has many disadvantages. If you are interested, you can check the information on your own.


It should also be noted that many people are not very clear about the concepts of the framework and design patterns, and they are particularly easy to confuse the two. Therefore, many people who do not have a deep understanding of MVC think that MVC is a design pattern. In fact, they are two completely different things. In simple terms, the framework is usually code reuse, while the design pattern is design reuse. The architecture is between the two. Some code is reused, some design is reused, and sometimes can be analyzed and reused.


In a word, the framework is wise and used to divide software design into different parts. The design mode is a small skill. It puts forward solutions to specific problems to improve code reuse rate and reduce Coupling Degree.

.

MVC is only a Framework, and there are many other commonly used frameworks, such as Struts, Spring, Zend Framework, and. net mvc. We look forward to learning and using them.

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.