ASP.NET MVC Overview前端
The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating MVC-based Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in theSystem.Web.Mvc namespace and is a fundamental, supported part of the System.Web namespace.
MVC is a standard design pattern that many developers are familiar with. Some types of Web applications will benefit from the MVC framework. Others will continue to use the traditional ASP.NET application pattern that is based on Web Forms and postbacks. Other types of Web applications will combine the two approaches; neither approach excludes the other. 數據庫
Figure 01: Invoking a controller action that expects a parameter value (Click to view full-size image)express
- Models. Model objects are the parts of the application that implement the logic for the application s data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in SQL Server.
In small applications, the model is often a conceptual separation instead of a physical one. For example, if the application only reads a data set and sends it to the view, the application does not have a physical model layer and associated classes. In that case, the data set takes on the role of a model object.設計模式
- Views. Views are the components that display the application s user interface (UI). Typically, this UI is created from the model data. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Products object.
- Controllers. Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction. For example, the controller handles query-string values, and passes these values to the model, which in turn queries the database by using the values.
- The MVC pattern helps you create applications that separate the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The pattern specifies where each kind of logic should be located in the application. The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps you manage complexity when you build an application, because it enables you to focus on one aspect of the implementation at a time. For example, you can focus on the view without depending on the business logic.
In addition to managing complexity, the MVC pattern makes it easier to test applications than it is to test a Web Forms-based ASP.NET Web application. For example, in a Web Forms-based ASP.NET Web application, a single class is used both to display output and to respond to user input. Writing automated tests for Web Forms-based ASP.NET applications can be complex, because to test an individual page, you must instantiate the page class, all its child controls, and additional dependent classes in the application. Because so many classes are instantiated to run the page, it can be hard to write tests that focus exclusively on individual parts of the application. Tests for Web Forms-based ASP.NET applications can therefore be more difficult to implement than tests in an MVC application. Moreover, tests in a Web Forms-based ASP.NET application require a Web server. The MVC framework decouples the components and makes heavy use of interfaces, which makes it possible to test individual components in isolation from the rest of the framework.
The loose coupling between the three main components of an MVC application also promotes parallel development. For instance, one developer can work on the view, a second developer can work on the controller logic, and a third developer can focus on the business logic in the model.
Deciding When to Create an MVC Application
You must consider carefully whether to implement a Web application by using either the ASP.NET MVC framework or the ASP.NET Web Forms model. The MVC framework does not replace the Web Forms model; you can use either framework for Web applications. (If you have existing Web Forms-based applications, these continue to work exactly as they always have.)
Before you decide to use the MVC framework or the Web Forms model for a specific Web site, weigh the advantages of each approach. api
Advantages of an MVC-Based Web Application
The ASP.NET MVC framework offers the following advantages:服務器
- It makes it easier to manage complexity by dividing an application into the model, the view, and the controller.
- It does not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application.
- It uses a Front Controller pattern that processes Web application requests through a single controller. This enables you to design an application that supports a rich routing infrastructure. For more information, seeFront Controller on the MSDN Web site.
- It provides better support for test-driven development (TDD).
- It works well for Web applications that are supported by large teams of developers and Web designers who need a high degree of control over the application behavior.
Advantages of a Web Forms-Based Web Application
The Web Forms-based framework offers the following advantages:網絡
- It supports an event model that preserves state over HTTP, which benefits line-of-business Web application development. The Web Forms-based application provides dozens of events that are supported in hundreds of server controls.
- It uses a Page Controller pattern that adds functionality to individual pages. For more information, see Page Controller on the MSDN Web site.
- It uses view state or server-based forms, which can make managing state information easier.
- It works well for small teams of Web developers and designers who want to take advantage of the large number of components available for rapid application development.
- In general, it is less complex for application development, because the components (the Page class, controls, and so on) are tightly integrated and usually require less code than the MVC model.
Features of the ASP.NET MVC Framework
The ASP.NET MVC framework provides the following features:session
- Separation of application tasks (input logic, business logic, and UI logic), testability, and test-driven development (TDD) by default. All core contracts in the MVC framework are interface-based and can be tested by using mock objects, which are simulated objects that imitate the behavior of actual objects in the application. You can unit-test the application without having to run the controllers in an ASP.NET process, which makes unit testing fast and flexible. You can use any unit-testing framework that is compatible with the .NET Framework.
- An extensible and pluggable framework. The components of the ASP.NET MVC framework are designed so that they can be easily replaced or customized. You can plug in your own view engine, URL routing policy, action-method parameter serialization, and other components. The ASP.NET MVC framework also supports the use of Dependency Injection (DI) and Inversion of Control (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 objects should get the second object from an outside source such as a configuration file. This makes testing easier.
- A powerful URL-mapping component that lets you build applications that have comprehensible and searchable URLs. URLs do not have to include file-name extensions, and are designed to support URL naming patterns that work well for search engine optimization (SEO) and representational state transfer (REST) addressing.
- Support for using the markup in existing ASP.NET page (.aspx files), user control (.ascx files), and master page (.master files) markup files as view templates. You can use existing ASP.NET features with the ASP.NET MVC framework, such as nested master pages, in-line expressions (<%= %>), declarative server controls, templates, data-binding, localization, and so on.
- Support for existing ASP.NET features. ASP.NET MVC lets you use features such as forms authentication and Windows authentication, URL authorization, membership and roles, output and data caching, session and profile state management, health monitoring, the configuration system, and the provider architecture.
模型-視圖-控制器(MVC)體系結構模式將應用程序分爲三個主要組件:模型、視圖和控制器。ASP。淨MVC框架提供了一個替代ASP。淨Web表單建立基於mvc Web應用程序的模式。ASP。淨MVC框架是一個輕量級的、高度可測試的報告框架,(與Web表單的應用程序)是結合現有的ASP。網絡功能,如主頁面和會員制的身份驗證。theSystem.Web MVC框架的定義。Mvc命名空間,是一項基本支持system .命名空間的一部分。
MVC是一個標準的設計模式,許多開發人員都熟悉。某些類型的Web應用程序將受益於MVC框架。別人會繼續使用傳統的ASP。網絡應用程序模式是基於Web表單,回發。其餘類型的Web應用程序將結合這兩種方法,也不排除其餘的方法。mvc
圖1:預計參數值調用控制器操做(點擊查看全尺寸圖片)
模型。模型對象的部分應用程序,實現應用程序的邏輯數據域。一般,模型對象檢索和模型狀態存儲在一個數據庫中。例如,一個產品對象可能會從數據庫中檢索信息,操做它,而後寫回產品的更新信息表在SQL Server。
在小應用程序中,模型一般是一個概念而不是物理分離。例如,若是應用程序只讀取一組數據並將其發送給視圖,應用程序沒有物理模型層和相關的類。在這種狀況下,數據集模型對象的角色。
的觀點。視圖是顯示的組件應用程序用戶界面(UI)。一般,這個UI建立從模型數據。一個例子將是一個產品表的編輯視圖顯示文本框、下拉列表、複選框基於產品對象的當前狀態。
控制器。控制器的組件處理用戶交互,使用模型,並最終選擇一個視圖顯示UI的呈現。在MVC應用程序中,視圖只顯示信息;控制器處理和響應用戶輸入和交互。例如,控制器處理查詢字符串值,並將這些值傳遞給模型,進而查詢數據庫使用的值。app
MVC模式能夠幫助您建立單獨的應用程序應用程序的不一樣方面(輸入邏輯、業務邏輯和UI邏輯),同時提供這些元素之間的鬆散耦合。的模式指定各類邏輯應該位於應用程序。UI邏輯屬於視圖。輸入邏輯屬於控制器。屬於業務邏輯模型。這種分離有助於管理複雜性構建一個應用程序時,由於它使您能夠專一於實現的一個方面。例如,您能夠專一於視圖沒有根據業務邏輯。
除了管理複雜性,MVC模式更易於測試應用程序比測試一個Web表單的ASP。淨的Web應用程序。例如,在一個Web表單的ASP。淨的Web應用程序,使用一個類來顯示輸出和響應用戶輸入。Web表單的ASP編寫自動化測試。網絡應用程序能夠是複雜的,由於測試單個頁面,您必須實例化頁面類,它的全部子控件,額外的依賴類的應用程序。由於不少類實例化運行頁面,很難編寫測試,專一於應用程序的各個部分。測試Web表單的ASP。網絡應用程序能夠所以比測試更難以實現MVC應用程序。此外,測試一個Web表單的ASP。網絡應用程序須要一個Web服務器。MVC框架解耦的組件,使大量使用接口,這使得它能夠單獨測試單個組件的框架。
三個主要組件之間的鬆散耦合的MVC應用程序也促進並行開發。例如,一個開發人員能夠在視圖上,第二個開發人員可使用控制器邏輯,和第三個開發人員能夠專一於業務邏輯的模型。
決定什麼時候建立一個MVC應用程序你必須仔細考慮是否要實現一個Web應用程序經過使用ASP。淨MVC框架或ASP。淨Web表單模型。MVC框架不能替代Web表單模型,您可使用Web應用程序的框架。(若是你有現有的Web表單的應用程序,這些繼續工做,由於他們老是有。)以前您決定使用MVC框架或一個特定網站的Web表單模型,權衡每種方法的優點。基於mvc Web應用程序的優勢ASP。NET MVC框架提供瞭如下優勢:這讓它更容易管理的複雜性,將應用程序劃分爲模型,視圖和控制器。它不使用視圖狀態或基於服務器的形式。這使得MVC框架適合開發人員想要徹底控制應用程序的行爲。它使用一個前端控制器模式處理Web應用程序的要求