MVC (Model-View-Controller) Structure Pattern puts an applicationProgramIt is split into three main components: model, view, and controller. When creating a web application based on the MVC framework, you can use the ASP. net mvc framework as an important candidate for the ASP. NET web form mode solution. Compared with web form-based applications, Asp. net MVC Framework is a lightweight and tested description framework, which can be used with existing ASP. NET features (such as master pages and membership-based authentication) are tightly integrated. The MVC framework is defined in the namespace system. Web. MVC.
For a long time, MVC is a standard design model that many developers are familiar. Some types of Web applications benefit from the MVC framework, while others will continue to use traditional ASP Based on Web forms and return technology.. NET application mode. Some Types of Web applications may be used together. In short, the MVC framework scheme and the web form scheme are not mutually exclusive.
In general, the MVC framework includes the following components:
(1) model.Model objects are part of the data domain logic of applications. Typically, model objects are also responsible for retrieving and storing model statuses from databases. For example, a product object may retrieve information from a database, operate on the data, and write the update information back to a product table in the SQL Server database.
Tip: In small applications, a model is often a conceptual separation rather than a physical concept. For example, if an application reads only one dataset and sends it to the view, the application does not have to create a physical model layer and associated classes. In this case, the data directly assumes the role of a model object.
(2) view.A view is a component that displays the user interface (UI) of an application. In typical cases, this UI is created based on model data. A typical example is the editing view of a product table. In this view, you can display text boxes, drop-down lists, check boxes, and other controls based on the current status of a product object.
(3) controller.The Controller component is responsible for processing user interaction and operating models, and finally selects and generates a view to display the UI. In an MVC application, the view is only responsible for displaying information, and the Controller is responsible for processing and responding to user input and interaction. For example, the controller processes the query string values, transmits these values to the model, and then uses this value in the model for database query.
The MVC mode separates different aspects of the application (input logic, business logic, and UI logic) and provides loose coupling between these elements. The MVC pattern requires that each different logic should exist in a specific location in the application. For example, the UI Logic belongs to the view, the input Logic belongs to the Controller, and the business logic belongs to the model. This separation helps you to manage the complexity of building an application because it enables you to focus only on one aspect of implementation at a specific time point. For example, you can focus on the view instead of the business logic.
In addition to management complexity, compared to testing a traditional ASP. NET web application, MVC-based development makes it easier to test the application. For example, in a traditional ASP. NET web application, a single class is used to display the output and respond to user input. Therefore, it is a traditional ASP. NET application coding automation testing may be very complicated, because in order to test a single page, you must instantiate the page class, all its child controls, and other dependent classes in the application. In short, you need to instantiate a large number of classes to run the page, so in this case it is much more difficult to write a test for a single part of the application. Therefore, testing traditional ASP. NET applications is much more complex and difficult than testing an application developed using the MVC framework. In addition, a traditional ASP. NET application also needs to use a web server during testing. In contrast, the MVC Framework separates components and uses a large number of interfaces, which makes it easier to test a single component independently from other parts of the framework.
In addition, the loose coupling between the three main components of an MVC application also promotes parallel development. For example, a developer can develop a view, while another developer is developing the Controller logic. The third programmer can focus on the development of the business logic of the model.
Advantages of Web applications based on MVC Framework
The ASP. net mvc Framework provides the following advantages:
By separating an application into independent models, views, and controllers, the management of application complexity is further improved.
View status or server-based form technology is no longer used. This makes the MVC framework especially beneficial to developers who want to fully control the behavior of an application.
Uses a front-end controller mode and uses a single controller to process web application requests. This feature makes it possible to design an application that supports an extremely rich Routing infrastructure. For more information about front-end controllers, see http://go.microsoft.com/fwlink? Linkid = 106357.
Provide strong support for test-driven development (TDD.
Advantages of Web applications based on Web Forms
The traditional web form-based framework provides the following advantages:
Supports event models. This model can retain the status during HTTP transmission, which is very beneficial to the development of online business Web applications. Web form-based applications provide a large number of events, all of which are supported by hundreds of server controls.
A page controller mode can be used to add many features to a single page. For more information about the page controller, see http://msdn.microsoft.com/zh-cn/library/ms978764.aspx.
Using view status or server-based forms makes it easier to manage status information.
ASP. net mvc framework features
In summary, the ASP. net mvc Framework provides the following important features:
Separates basic application tasks (input logic, business logic, and UI logic) and supports test and test-driven development (TDD ). All core contracts in the MVC Framework are based on interfaces, so it is very helpful to use mock objects (which are the simulated objects of some simulated applications) to test the project. You can perform unit tests on applications without having to run controllers in real ASP. NET processes, which speeds up unit tests and makes them flexible. You can use any unit test framework compatible with the. NET Framework to test your project.
Provides a scalable and pluggable framework. The Design of ASP. net mvc framework components allows users to easily replace or customize them. You can design and add your own view engine, URL routing policy, serialization of behavior method parameters, and other components. The ASP. net mvc framework also supports the use of existing dependency injection ("dependency injection", "Di" for short) and control inversion (IOC) Container models. Di allows you to inject objects into a class, instead of relying on the class to create the object itself. IOC specifies that if an object requires another object, the first object should obtain the second object from an external source (such as a configuration file. This support is very helpful for engineering testing.
Implements a powerful URL ing component that allows you to build understandable and searchable URLs in your applications (rather than the extremely complex expressions in many web application URLs ). Nowadays, the URL does not need to include the file extension, and its design also supports the URL naming mode. Therefore, this pattern facilitates search engine optimization (SEO) and rest (expressive state transfer) addressing.
You can use tags in existing ASP. NET pages (. aspx files), user controls (. ascx files), and master pages (. Master files) as view templates. You can use ASP.. Net MVC framework continues to use existing ASP. NET features, such as nested master pages, inline expressions (<% = %>), declarative server controls, templates, data binding, localization, and so on.
Supports existing ASP. NET features. ASP. net MVC Framework supports you to use such as form and Windows authentication, URL Authorization, members and roles, output buffering and data buffering, session and configuration status management, health monitoring, Configuration System, provider architecture, and other ASP. NET features.
URL ing
In typical cases, in an ASP. NET Website, URLs are mapped to files stored on disks (mainly. aspx files ). These. aspx files will includeCodeOr mark.
The ASP. net mvc framework maps URLs to server code in a way different from ASP. NET Website applications. The MVC Framework does not map URLs to ASP. NET pages or HTTP processor programs, but directly maps URLs to specific controller classes. Then, the Controller class is responsible for processing incoming requests, such as user input and interaction, and executing appropriate applications and data logic based on user input. Typically, a controller class calls a separate view component to generate the corresponding HTML output as a response to the request sent by the client.
In the ASP. net mvc Framework, the model, view, and controller components are separated. Typically, the model component maintains the state of persistent data in the database, while the view component is selected by the Controller and finally generates the corresponding UI on the client. By default, Asp. net MVC framework uses existing ASP. NET page (. aspx), master page (. master) and user controls (. ascx) type as the output form on the browser side. The Controller component locates the appropriate behavior methods in the controller, obtains the parameter value used as the behavior method, processes all possible errors during execution, and generates the required view. Each group of components is stored in a separate folder of an MVC web application project.
By providing a URL ing engine, ASP. net mvc Framework provides considerable flexibility in ing URLs to Controller classes. You can use this ing engine to define routing rules. Then the ASP. net mvc framework uses these routing rules to calculate the sent URL request and select the Controller to execute. You can also enable the routing engine to automatically analyze the variables defined in the URL and enable the ASP. net mvc Framework to pass the content to the Controller as parameters.
MVC Framework and page return
the ASP. net mvc Framework does not use the traditional ASP. NET return model to achieve interaction with the server. Instead, all end-user interactions are routed to a controller class. This helps to keep the separation of UI logic and business logic, and helps test the purpose. The result is that ASP. NET view status and ASP. NET page lifecycle events are not integrated with MVC-based views.