您的位置:首页 > 编程语言 > ASP

[翻译]ASP.NET MVC CodePlex Preview 5 更新细节(未完成)

2008-08-29 22:47 579 查看
ASP.NET MVC CodePlex Preview 5 更新细节

本文档描述了ASP.NET MVC framework between the CodePlex Preview5(一下简称P5)相较于Preview4之间的变化。同时也说明了您如何改变现有的MVC应用来适应这些变化。

.htm#_Toc207690402]

都有啥新鲜的?

这次的P5仍然是一个临时版本。它包含了一些新特性以及性能改善。ASP.NET MVC小组想一如往常的借此从社区收集反馈,如果您有任何的建议或者意见请随时到(http://forums.asp.net/1146.aspx)进行反馈。

注意:在您安装P5之前,请确认已经卸载了以往任何版本的ASP.NET MVC Preview。同时关闭所有的Visual Studio 2008运行实例

本次更新中提供的新特性和相关信息如下

ViewEngine 的改进

Added global registration of view engines. It is no longer the controller’s responsibility to determine which view engine to use to render a view (though the controller can still select a view engine if needed). Instead, when a controller wants to render a view, it returns a ViewResult object (via the View method).

增加了全局注册视图引擎功能。controller不再负责决定使用哪种视图引擎来呈现视图(当您确实需要时,在controller中仍然可以选择视图引擎)。反之,当controller要呈现视图时,它返回一个ViewResult对象(通过View方法)。

默认情况下,这个ViewResult调用CompositeViewEngine寻找视图。CompositeViewEngine从继承了IViewEngine的ViewEngines.Engines静态集合中遍历视图引擎,从而决定使用哪种视图引擎呈现特定的视图。第一个符合要求的引擎返回一个IView实例来呈现视图。

IViewEngine接口中增加了RenderPartial方法.现在视图引擎提供了呈现部分视图的概念。这是为了与Html.RenderPartial的helper方法协同工作。

Helper 的改进

Added support for rendering partial views. You have several options for passing view data to the partial view. The partial view can be rendered by a different view engine than the containing view. Usage is <% Html.RenderPartial(...); %>. This method does not return a string, but instead renders to the underlying response’s supplied TextWriter instance.

为呈现部分视图提供支持。有很多人希望向部分视图传递数据。通过<%Html.RenderPartial(...);%>,部分视图可以呈现在各种视图引擎中。这个方法不返回一个字符串而是呈现更为底层的TextWriter实例。

Added a parameter to specify a default option label for DropDownList controls. Specifying a value for the optionLabel argument to

为DropDownList控件增加了一个用来设置默认选项的参数。这可以让您为DropDownList方法指定呈现一个默认的选项,这个默认选项的value为空字符串,而它显示的具体内容取决于您提供的参数。这样,被提供此参数的drop-drow list控件的默认选项并不具有有效值,它的存在仅仅是为了提供一些选项的说明。

将ASP.NET AJAX扩展方法被组织到新的命名空间下。这使得您可以在代码中轻松的完成对AJAX helper的扩展。

注意:如果您的项目从P4升级至P5却没有看到这些方法,请在您的应用程序中引入System.Web.MVC.Ajax命名空间,并切在web.config文件中加入下面高亮显示的内容。

<namespaces>

<add namespace="System.Web.Mvc"/>

<add namespace="System.Web.Mvc.Ajax"/>

<add namespace="Microsoft.Web.Mvc"/>

<add namespace="System.Web.Routing"/>

<add namespace="System.Linq"/>

<add namespace="System.Collections.Generic"/>

</namespaces>

Added helpers for RadioButton and TextArea controls and made overall improvements to other helpers. Improvements and changes to various helpers have been made. In some cases, overloads have been added or removed to avoid overload ambiguity. (See next point.)

在helper中增加了RadioButton和TextArea控件,同时对其他的helper做出了大量修改。我们对很多helper都进行了变更或改进,增加或者去掉了一些重载来避免有歧义的重载。(见下一条)

为了避免歧义,我们去掉了一些helper方法的重载。我们为很多helper方法去掉了一些重载并且将value参数从object类型更改为string类型。这是为了避免出现注入如您调用Html.Hidden("name",123)且希望它的value为"123"却被某些重载接受为一个object而错误的呈现出HTML属性这类的情况。(因为123是一个对象)

Controller 及 Filter 的改进

为action方法的参数提供了数组支持。具有相同名称的元素数据可以被转化成数组传递给action方法。举个例子,比如您有三个表单对象(就拿textbox说事吧)都叫"color",且具有不同的颜色名称做为值,当您的表单提交到action时而同时action方法存在一个"color"的string型参数时,它们的值将被自动的组织到一个叫color的数组中。

从action filter context对象中去掉了ActionMethod属性。这让我们的公共API尽可能的减少了耦合。

为自定义model binder提供支持。自定义binder允许您定义复合的数据类型做为action方法的参数。要使用这个特性,请为复杂数据类型或参数标记[ModelBinder(...)].

增加了一个IActionInvoker接口。Controller类不再依赖于一个明确的ControllerActionInvoker类。现在它继承IActionInvoker接口,这个开放性的行为将对未来的扩展产生深远的影响。

Added an UpdateMode method to the Controller class. 为Controller类添加了一个UpdateMode方法。这个方法用来更新model中经HTTP协议或route数据提供的属性值。当您视图更新model时,提交值The method can be used to update the property values of a model object based on either the values posted in the HTTP request or on route data. When you attempt to update the model, errors that occur when the post values are converted to the property type are recorded in the new ModelStateDictionary instance, which stores information about the model.

Changed HandleErrorAttribute so that it does not handle exceptions when HttpContext.IsCustomErrorEnabled is false. This allows exceptions to propagate to the standard ASP.NET error page (the “Yellow Screen of Death”) during development, which provides you with more information than an error page that is intended for users. For more information, see Eilon Lipton’s blog post "HttpContext.IsCustomErrorEnabled - One of ASP.NET's hidden gems".

Added a new AcceptVerbs attribute. Thisattribute allows you to specify which HTTP verbs an action method will handle. If the current request’s HTTP method does not match the list of verbs that are specified in the attribute, from the perspective of the action invoker, the method does not exist. This allows you to have two action methods of the same name, as long as they respond to different HTTP verbs. This attribute inherits from the abstract ActionSelectionAttribute class, which can be used to control how action methods are chosen to respond to a request.

Added a new ActionName attribute. Thisattribute allows you to assign an action name to a method. By default, if a method does not have this attribute applied, the method name itself serves as the action name.

For example, you can map a method named ViewName to respond to the action View. This attribute and the AcceptVerbs attribute can be used together, as shown in the following example:

[AcceptVerb("GET")]

public ActionResult MyAction(...);

[ActionName("MyAction"), AcceptVerb("POST")]

public ActionResult MyAction_FormSubmit(...);

[AcceptVerb("GET")]

public ActionResult OtherActionOverloaded(string id);

[AcceptVerb("POST")]

public ActionResult OtherActionOverloaded(string id, ...);

A GET request for /controller/MyAction is handled by the first method, while a POST request for /controller/MyAction is handled by the second method.

Likewise, even though the code contains two methods named OtherActionOverloaded, which is normally not allowed for actions, a GET request for /controller/OtherActionOverloaded only matches one of those methods (due to the AcceptVerbsAttribute) and no exception is thrown. A POST request for the same URL matches the second OtherActionOverloaded method.

Known Issues and Breaking Changes

Controllers

The abstract Controller class now derives from ControllerBase, which contains useful properties such as ViewData and TempData. Additionally, the ControllerContext.Controller property is a ControllerBase reference rather than an IController reference. This change was made so that filters can access ViewData and TempData instances without casting the ControlllerContext.Controller property.

To support this change, the protected Controller.Execute(ControllerContext controllerContext) method was removed. If you have a custom controller that overrides the Execute(ControllerContext controllerContext) method, change it to override the ExecuteCore method instead.

Controllers that require additional initialization steps to perform tasks such as setting the thread culture or assigning a custom provider for TempData can do so by overriding the Initialize(RequestContext requestContext) method. The implementation in the base class ensures that the ControllerContext property will be set.

View Engines

我们改变了IViewEngine接口。现在,当他发现视图时并不呈现他们。

Helper Methods

我们干掉了一些helper的重载方法。这可能会导致升级后您现有的视图出现问题。同时,ASP.NET AJAX helper方法的命名空间也重新组织过了。相关信息请看上文。

Visual Studio 2008 Service Pack 1 Beta Release

这个版本的ASP.NET MVC并不能应用于Visual Studio 2008 SP1 Beta。如果您在Visual Studio 2008环境下使用P5,请确认您的Visual Studio 2008已经升级到SP1 RTM。

Visual Studio 2008 Express Edition Service Pack 1

同样,如果您正在使用Visual Studio 2008 Express,您也需要先将其升级至SP1

您可以在如下地址得到SP1:

http://www.microsoft.com/express/download/

您也可以从 Scott Guthrie's 的博客文章:ASP.NET MVC Support with Visual Web Developer 2008 Express中得到更多的相关信息。

从 Preview 4 升级至 Preview 5

· 重新引用如下属于P5的新程序集:

· System.Web.Abstractions.dll

· System.Web.Routing.dll

· System.Web.Mvc.dll

默认情况下,这些程序集位于%ProgramFiles%"Microsoft ASP.NET"ASP.NET MVC CodePlex Preview 5文件夹中

· 由于System.Web.Abstractions和System.Web.Routing程序集的版本号已经返璞归真至3.5.0.0(我们在ASP.NET 3.5 SP1中提供了这些程序集精确的二进制拷贝),您必须更改web.config文件以适应升级。在web.config文件中找到如下片段:

System.Web.Routing, Version=0.0.0.0

Replace it with the following string:

System.Web.Routing, Version=3.5.0.0

Search for the following string:

System.Web.Abstractions, Version=0.0.0.0

Replace it with the following string:

System.Web.Abstractions, Version=3.5.0.0

· 搞定这些以后,重新编译您的应用程序并解决其他的一些编译问题。大部分的问题应该是由以上描述的这些变更所引起的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: