[一步一步MVC]第六回:什么是MVC(上)?

系列文章导航:

[一步一步MVC]第一回:使用ActionSelector控制Action的选择

[一步一步MVC]第二回:还是ActionFilter,实现对业务逻辑的统一Authorize处理

[一步一步MVC]第三回:MVC范例大观园

[一步一步MVC]第四回:漫谈ActionLink,有时“胡搅蛮缠”

[一步一步MVC]第五回:让TagBuilder丰富你的HtmlHelper

[一步一步MVC]第六回:什么是MVC(上)?


引言

所谓MVC,其实就是M、V、C而已。归根揭底,MVC是一种表现模式,是一种软件架构模式。其中有几个重要的概念:

  • M,Model, 引用系统数据,管理系统功能并通知View更改用户操作。
  • V,View,就是用户接口,用于显示数据。
  • C,Controller ,将用户操作映射到Model,并操作视图。
  • R,Routing ,MVC的奥秘在于通过Routing实现了对URL的路由选择,完成了上述3个基本概念的基础逻辑。

我们先来了解这几个概念之间的联系。

 

o_anytao-mvc-09-01[1]

对MVC而言,分离是最大的优点,尤其是Model将不依赖于Controller和View,对于隔离应用、进行UI测试打下很好的架构级支持。

MVC Execution Process

关于MVC的执行过程,我们就不多言了,从MSDN获取的执行过程可以被解析为:

o_anytao-mvc-09-02[1] 

在MVC模式下,不同于WebForm时代,业务逻辑的处理和HTML的输出不是View(或Page)一个人的事儿,这些逻辑被清晰的分解为M、V和C的逻辑,具体的执行流程为:

ASP.NET MVC Execution Process

Stage

Details

Receive first request for the application

In the Global.asax file, Route objects are added to the RouteTable object.

void Application_Start(object sender, EventArgs e) {    RegisterRoutes(RouteTable.Routes);}public static void RegisterRoutes(RouteCollection routes){    routes.Add(new Route    (         "Category/{action}/{categoryName}"         , new CategoryRouteHandler()    ));}

Perform routing

The UrlRoutingModule module uses the first matching Route object in the RouteTable collection to create the RouteData object, which it then uses to create a RequestContext object.

 

Create MVC request handler

The MvcRouteHandler object creates an instance of the MvcHandler class and passes the RequestContext instance to the handler.

Create controller

The MvcHandler object uses the RequestContext instance to identify the IControllerFactory object (typically an instance of the DefaultControllerFactory class) to create the controller instance with.

Execute controller

The MvcHandler instance calls the controller's Execute method.

Invoke action

For controllers that inherit from the ControllerBase class, the ControllerActionInvoker object that is associated with the controller determines which action method of the controller class to call, and then calls that method.

Execute result

The action method receives user input, prepares the appropriate response data, and then executes the result by returning a result type. The built-in result types that can be executed include the following: ViewResult (which renders a view and is the most-often used result type), RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult.

NET技术[一步一步MVC]第六回:什么是MVC(上)?,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。