ASP.NET MVC Overview
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)
- 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.
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:
- 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应用程序将结合这两种方法,也不排除其他的方法。
图1:预计参数值调用控制器操作(点击查看全尺寸图片)
模型。模型对象的部分应用程序,实现应用程序的逻辑数据域。通常,模型对象检索和模型状态存储在一个数据库中。例如,一个产品对象可能会从数据库中检索信息,操作它,然后写回产品的更新信息表在SQL Server。
在小应用程序中,模型通常是一个概念而不是物理分离。例如,如果应用程序只读取一组数据并将其发送给视图,应用程序没有物理模型层和相关的类。在这种情况下,数据集模型对象的角色。
的观点。视图是显示的组件应用程序用户界面(UI)。通常,这个UI创建从模型数据。一个例子将是一个产品表的编辑视图显示文本框、下拉列表、复选框基于产品对象的当前状态。
控制器。控制器的组件处理用户交互,使用模型,并最终选择一个视图显示UI的呈现。在MVC应用程序中,视图只显示信息;控制器处理和响应用户输入和交互。例如,控制器处理查询字符串值,并将这些值传递给模型,进而查询数据库使用的值。
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应用程序的要求
ASP.NET MVC Overview的更多相关文章
- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
The Contoso University sample web application demonstrates how to create ASP.NET MVC 5 applications ...
- Asp.net Mvc Entity Framework Code First 数据库迁移
1.创建Mvc项目 2.安装Entity Framework 2.1.如下图打开程序包管理器控制台: 2.2.输入命令Install-Package EntityFramework,即可安装Entit ...
- mark asp.net mvc
http://weblogs.asp.net/scottgu/Tags/MVC http://weblogs.asp.net/scottgu/asp-net-mvc-framework-part-1 ...
- ASP.NET MVC 随想录—— 使用ASP.NET Identity实现基于声明的授权,高级篇
在这篇文章中,我将继续ASP.NET Identity 之旅,这也是ASP.NET Identity 三部曲的最后一篇.在本文中,将为大家介绍ASP.NET Identity 的高级功能,它支持声明式 ...
- ASP.NET MVC系列:开始
创建Asp.Net MVC项目 从visual studio主界面开始菜单中点击“新建项目”
- ASP.NET MVC系列:添加控制器
基于MVC的应用程序包含三个部分 Models(模型):对应用程序的数据进行处理 Views(视图):动态生成HTML,显示数据 Controllers(控制器):应用程序中处理用户交互的部分,处理浏 ...
- ASP.NET MVC系列:添加视图
虽然在上一篇文章中我们知道通过控制器可以在浏览器输出HTML页面,但是这不是控制器主要干的事,因为页面上我为还要做很多好看的特效,页面展示的事情当然交给视图来做了:下面我们就来看看如何添加一个视图 添 ...
- ASP.NET MVC系列:添加模型
模型(Model)是应用程序中用于处理应用程序数据逻辑的部分;通常模型对象在数据库中存取数据 添加模型类 在解决方案中右击Models文件夹,然后选择“添加”,在“添加”项里选择“类”:或者选中Mod ...
- ASP.NET MVC系列:从Controller访问Model数据
在项目解决方案中,添加一个MoviesController控制器,选择对应的模板,和模型类以及数据上下文:关于如何添加模型类和数据上下文,我们在ASP.NET MVC系列:添加模型中已经介绍过
随机推荐
- js中对闭包的理解
本人丝一枚,在刚刚过去的javascript学习中,对闭包这个知识真是,听课两分钟,懵逼一整天.今天闲来没事,看了下闭包.话不多说先上代码. <!DOCTYPE html> <htm ...
- Mustache
简要介绍 玩过node的人应该都知道ejs,jade. mustache和他们一样都是模板渲染引擎,我个人喜欢mustache,因为他非常简洁,代码量才600多行. mustache既可以在前端使用, ...
- 安卓热更新之Nuwa实现步骤
安卓热更新之Nuwa实现步骤 最近热更新热修复的功能在安卓应用上越发火热,终于我的产品也提出了相应的需求. 经过两天的研究,搞定了这个功能,在这里还要多谢大神们的博客,大神们的原理分析很到位,不过对于 ...
- windows server 2008 集成raid卡驱动
给服务器安装2008系统,一般都需要通过引导盘和操作系统盘来进行安装,安装过程比较繁琐时间也比较长,于是就想做一个集成了服务器驱动的2008系统盘,这样就可以直接用光盘安装,简单方便,第一步需要解决的 ...
- mysql 高性能
第一章节:共享锁(读锁),排他锁(写锁) 查询数据表所使用的存储引擎:show table status like '表名' \G 转换数据表的存储引擎:alter table 表名 engine=引 ...
- A-frame_02
A-Frame 让我们能够仅仅通过几行HTML代码创建出可以运行在桌面, 虚拟眼镜, 以及手机上的VR场景. 而且因为这个框架是基于HTML的, 我们也可以像一般的HTML元素一样配合JavaScri ...
- Sql Server 判断表或数据库是否存在
发布:thebaby 来源:脚本学堂 [大 中 小] 本文详细介绍了,在sql server中判断数据库或表是否存在的方法,有理论有实例,有需要的朋友可以参考下,一定有帮助的.原文地址:h ...
- C++ 实现设计模式之观察者模式
1. 什么是观察者模式? 观察者模式(有时又被称为发布-订阅Subscribe>模式.模型-视图View>模式.源-收听者Listener>模式或从属者模式)是软件设计模式的一种.在 ...
- HIVE中内连接和左半连接不一致问题
一.理论 HIVE中都是按等值连接来统计的,理论上两种写法统计结果应该是一致的: 二.实际情况 但实际使用中发现两种写法会返回的结果,总会有一些差距虽然差别不大,但让人很是困惑. 三.原因 当使用jo ...
- 【TLD】标签库描述符
tld是taglib description 的缩写 定制标签拼图中的最后一块是TLD(标签库描述符)文件.创建的每个标签都必须在一个TLD文件中声明,而且这一文件还必须连同标签的Java处理器类一起 ...