ASP.NET MVC 源码更新预览

原文请看:ScottGu的原文ASP.NET MVC Source Refresh Preview

这里只是概括说一下。

注:A few hours ago we published a refresh of the ASP.NET MVC source code on the site.  This source refresh is not an official new ASP.NET MVC preview release - instead it is an interim drop that provides a look at the current state of the source tree. (直接帖原文了,就是说这不是一个官方发布的预览版,只是暂时的,是对MVC Preview 3目前状况的一个预览)

ASP.NET MVC源码更新的改进

这个星期ASP.NET MVC的更新(你可以从这里下载)包括不少的改进,其中的一些包括:

  • 除了更新ASP.NET MVC框架的源码外,也发布了我们用来进行单元测试的源码。这些测试是使用MSTest和开源的Moq框架。一个用来单元测试的VS 2008项目已经可以在你本地的VS 2008 IDE中很容易的来编译和运行。

  • 对测试Controller类测试提供了更简单的支持。你现在可以单元测试普通的Controller而不用mock任何对象(下面会有详细的介绍)

  • 添加了若干很漂亮的特性和改经了URL routing系统的可用性(更多的介绍看下面)。

创建一个新的ASP.NET MVC项目

你可以通过下载MVC源码并在本地编译它来创建你的ASP.NET MVC程序集副本,或者你也可以选择去下载一个VS的Template模板包来很容易的创建你的新ASP.NET MVC项目.

在你安装ASP.NET MVC源码更新后,你就可以在新建项目中看到一个新的“ASP.NET MVC Application”项目:

1

这个新的"My Templates"版本MVC项目模板就在MVC2(MVC Preview 2)的模板(就在红框的上面)下面。这就允许你在同一台电脑上很安全的创建不同版本的MVC项目。

当你用新的MVC模板创建一个新的MVC项目后,你会看到一个默认的好像下图所示的项目:

2

(目录结构根MVC2还是一样)

编译并运行可以看到:Home

3

About:

4

这个项目中的"HomeController"包含有两个Action方法,如下所示:(看出有什么不同了吧?)

5

Controller Changes with this ASP.NET MVC Drop

如果你很仔细的阅读上面的代码,你可以发现Controller类的默认实现已经改变了。

在MVC2中是好像下面这样实现的:

6

MVC feature team试验了一些新的idea:

  1. Controller类中的Action方法现在默认是返回一个"ActionResult"对象(代替之前的void).这个ActionResult对象指示了从一个Action中返回的结果(一个呈现的视图、一个重定向到的URL、另外一个要执行的Action/Route、等等)

  2. Controller基类中的 RenderView(), RedirectToAction(), and Redirect() helper methods 现在是返回类型为ActionResult的对象()(你可以进一步操作或者在Action方法中返回).

  3. The RenderView() helper method 现在可以不用明确的指定一个视图名来呈现。当你省略RenderView()方法中的要呈现的视图名的时候,RenderView()会呈现默认的视图。例如你Action()方法中调用没有参数的"RenderView()"方法的时候,就和"RenderView("About")"是一样的。

这从已有的MVC2项目中升级过来是很容易的,只需修改void为ActionResult,并在RenderView方法或者RedirectToAction方法前添加一个return。

 

从Action方法中返回ActionResult对象

为什么要改变Controller Action方法来默认返回ActionResult对象来代替void呢?有不少其他的流行的Web-MVC框架使用返回对象这个途径(包括 Django,Tapestry和其他的),而我们发现对于ASP.NET MVC这带来了一些非常好的益处:

  1. 这使你更干净和容易的支持对Controller进行单元测试。你可以很简单的使用Action方法中返回的ActionResult对象来进行单元测试(具体看下面)

  2. 这在输出结果依赖某个条件可能有两种不同的输出时(例如:条件A为真时重定向,否则呈现一个视图),能使Controller逻辑的目的清楚一点并更确定。这可以使写出来的复杂的Controller Action方法的代码更加可读。

  3. 这允许一些漂亮的复合情况,一个FilterActionAttribute可以取Action方法中的结果并在执行它之前修改/转换它。例如:ProductCatalog controller中一个“Browse”action返回一个表明它想呈现一个“List”的视图的RenderActionResult对象。在该Controller上声明的FilterActionAttribute就可以有一个机会来自定义指定的“List”是呈现客户喜欢的List-html.aspx还是List-xml.aspx MIME类型。

  4. 它为人们提供了一个非常好的扩张机制,以在将来可以添加额外的特性。新的ActionResult类型可以很容易的通过继承自ActionResult基类并重写"ExcuteResult"方法来实现。 It would be easy to create a "RenderFile()" helper method, for example, that a developer writing an action could call to return a new "FileActionResult" object.

  5. 在将来它将允许一些漂亮的异步执行情况。Action方法将会可以返回一个AsyncActionResult对象。

我们也会发布一个允许你可选择的使用"void"或者"ActionResut"的返回值的Controller基类sample。我们在这个更新版中故意不包含这个基类是因为我们想鼓励人们使用“ActionResult”的返回值并给我们反馈。

 

怎样对Controller Action方法进行单元测试

睡觉,明天看情况再补上,或者等等博客堂就会有翻译的了 -。-

 

MapRoute Helper Method

在MVC1和MVC2中routes添加到routes集合中是通过实例化一个Route对象,写到一个MvcRouteHandler类中,然后设置属性来声明route规则:

7

上面的代码在以后也是可以执行的。当然现在你可以使用“MapRoute”来很简单的做相同的工作:

8

MapRoute()方法是重载的并可以有两个、三个、四个参数(route name, URL syntax, URL parameter default, and URL parameter regular expression constraints).正则,哈哈,这个帅。

9

我们然后就可以参考这个"Products-Browse"规则来在我们的Controller和Action来为它产生URL。例如,我们可以用Html.RouteLink view helper来产生一个URL:10

产生的URL如下所示:

11 

Note: with this week's source drop you need to pass-in the controller and action parameters (in addition to the Category param) to the Html.RouteLink() helper to resolve the correct route URL to generate.  The ASP.NET MVC Preview 3 drop in a few weeks will not require this, and allow you to use the Html.RouteLink call exactly as I've written it above to resolve the route.

Other URL Route Mapping Features

This week's MVC source drop also supports a bunch of new URL route mapping features.  You can now include "-", ".", ";" or any other characters you want as part of your route rules.

For example, using a "-" separator you can now parse the language and locale values from your URLs separately using a rule like below:

This would pass appropriate "language", "locale", and "category" parameters to the ProductsController.Browse action method when invoked:

URL Route Rule Example URL Parameters Passed to Action method
{language}-{locale}/products/browse/{category} /en-us/products/browse/food language=en, locale=us, category=food
  /en-uk/products/browse/food language=en, locale=uk, category=food

Or you can use the "." file extension type at the end of a URL to determine whether to render back the result in either a XML or HTML format:

This would pass both "category" and a "format" parameters to the ProductsController.Browse action method when invoked:

URL Route Rule Example URL Parameters Passed to Action method
products/browse/{category}.{format} /products/browse/food.xml category=food, format=xml
  /products/browse/food.html category=food, format=html

ASP.NET MVC Preview 2 introduced wildcard route rules.  For example, you can indicate in a rule to pass all remaining URI content on as a named parameter to an action method:

This would pass a "contentUrl" parameter to the WikiController.DisplayPage action method when invoked:

URL Route Rule Example URL Parameters Passed to Action method
Wiki/Pages/{*contentUrl} /Wiki/Pages/People/Scott contentUrl="People/Scott"
  /Wiki/Pages/Countries/UK contentUrl="Countries/UK"

These wildcard routes continue to work fine with this week's preview - and are very useful to look at if you are building a blogging, wiki, cms or other content based system.

Note that in addition to using the new routing system for ASP.NET MVC scenarios, we are also now using the same routing system within ASP.NET Dynamic Data (which uses ASP.NET Web Forms).

对于URL Routing中的修改,可以看下微软的MVC主要负责人之一的Phil Haack先生的文章:

Routing中的一些修改 - You've been HAACKED中文版 - 博客堂
http://blog.joycode.com/haacked/archive/2008/04/14/115071.aspx

 

 

睡觉,后面的将就着看,不想看就等博客堂的中文版.  -。-

posted on 2008-04-17 23:53  Q.Lee.lulu  阅读(9523)  评论(29编辑  收藏  举报