ASP.NET MVC Preview 3 Release Notes

ASP.NET MVC Preview 3 Release Notes

This document describes changes that have been made to the ASP.NET MVC framework for the Preview 3 release. It also describes changes you must make in existing MVC applications to run with the new release.

Controller Changes. 2

Action Methods. 2

Action Filter Changes. 3

View Changes. 4

Routing Changes. 4

Other Changes. 5

Upgrading an Existing Preview2 Application to Preview 3. 6

Code Changes. 6

Configuration Changes. 7

Integrating with ASP.NET Dynamic Data.. 8


Controller Changes

The following changes were made to controllers.

Action Methods

Action methods now return an action result, which is an instance of the ActionResult class. This ActionResult object encapsulates the result from the action and indicates to the MVC framework what operation should be taken next, such as rendering a view, redirecting to a URL, and so on.

The following example shows how an action method that was written with the Preview 2 release might be rewritten for Preview 3. The Preview 2 version of the action method might look like the following:

public class HomeController : Controller {

  public void Index() {

    RenderView("Index");

  }

}

When the code is rewritten for the Preview 3 release, it might look like the following:

public class HomeController : Controller {

  public ActionResult Index() {

    return View("Index");

  }

}

Each action result is a type that inherits from ActionResult. The following table lists types in the Preview 3 release that inherit from ActionResult.

ActionResult Types

The following table lists ActionResult types.

Type

Notes

ViewResult

Renders the specified view to the response.

EmptyResult

Does nothing. Returned if the action method must return a null result.

RedirectResult

Performs an HTTP redirect to the specified URL.

RedirectToRouteResult

Given some routing values, uses the routing API to determine the URL and then redirects to that URL.

JsonResult

Serializes the specified ViewData object to JSON format.

ContentResult

Writes the specified text content to the response.

 

The following table lists helper methods in the Controller class that return these results.

Method

Notes

View

Returns a ViewResult instance.

Redirect

Redirects to the specified URL. Returns a RedirectResult instance.

RedirectToAction

Accepts an action (and optionally a controller) and redirects to another controller action. Returns a RedirectToRouteResult instance.

RedirectToRoute

Redirects to a URL that is determined by the routing API. For example, this method lets you specify a named route. Returns a RedirectToRouteResult instance.

Json

Returns a JsonResult instance.

Content

Sends text content to the response. Returns a ContentResult instance.

Implicit Action Results

If an action method returns null (or has a return type of void), the action invoker implicitly provides an EmptyResult instance, which does nothing. If an action method returns anything other than an ActionResult instance, the action invoker calls ToString(CultureInfo.InvariantCulture) on the instance and then wraps the return value with a ContentResult object, which writes the content to the response.

Action Filter Changes

The following changes have been made to action filters in the Preview 3 release.

·         A new IActionFilter interface for action filters has been added. The ActionFilterAttribute type implements IActionFilter.

·         Action filters now have four methods that represent the following possible interception points:

·         OnActionExecuting occurs just before the action method is called.

·         OnActionExecuted occurs after the action method is called, but before the result is executed—that is, before the view is rendered (in common scenarios).

·         OnResultExecuting occurs just before the result is executed—that is, before the view is rendered (in common scenarios).

·         OnResultExecuted occurs after the result is executed—that is, after the view is rendered (in common scenarios).

The OnResult* methods will not be called if an exception is not handled when the OnAction* methods are called, or during the action method itself.

View Changes

The following changes have been made to views in the Preview 3 release.

·         The following HTML helpers were updated with bug fixes:

·         TextBox

·         Hidden

·         Password

·         DropDownList

·         ListBox

 

·         A Model property was added to ViewDataDictionary. For ViewDataDictionary, the type of this property is System.Object. For ViewDataDictionary<T>, the type of this property is T.

·         The ViewData property of ViewPage<T> is no longer replaced by T. In Preview 2, the MVC framework replaced the ViewData property with the specified strongly typed view data (that is, the T in ViewPage<T>). In Preview 3, the Model property of ViewData is set to the instance of type T.

Routing Changes

The following changes were made to routes and routing.

·         MapRoute Extension. The framework now has a MapRoute extension method (an extension on RouteCollection). This extension makes it easier to declare MVC routes than in earlier releases. The following example shows how to map a route in earlier releases.

routes.Add("route-name", new Route("{locale}/{year}", new MvcRouteHandler()) {

  Defaults = new RouteValueDictionary(new {locale="en-US", year=DateTime.Now.Year.ToString()})

}

In the Preview 3 release, you can accomplish the same task by using the following code:

routes.MapRoute("route-name", "{locale}/{year}", new {locale="en-US", year=DateTime.Now.Year.ToString()});

·         An IRouteConstraint interface was added.

·         If a constraint value is specified as a string, the string is interpreted as a regular expression. If the constraint value is specified as an instance of IRouteConstraint, route processing calls the Match method of IRouteConstraint.

·         A new HttpMethodConstraint type was added, which changes the way you constrain routes on the HTTP method. Unlike previous versions of ASP.NET routing, in this release, the constraint name "httpMethod" is no longer special. Instead, use the HttpMethodConstraint to add a constraint based on HTTP verbs. The following example shows how to use the HttpMethodConstraint type.

routes.MapRoute("route-name"

  , "{controller}/update"

  ,  new {action = "update"}

  , new {httpMethod = new HttpMethodConstraint("PUT", "POST")});

Other Changes

·         The versions of the System.Web.Abstractions and System.Web.Routing assemblies that are included with the MVC project template have been changed to version 0.0.0 .0. The versions that are included in the Preview 3 release are newer than those that ship in the .NET Framework version 3.5 Service Pack 1 Beta. Therefore, they were assigned a private version number so that no conflict occurs between the assemblies in this release and the assemblies installed by the .NET Framework 3.5 SP1 Beta release.


Upgrading an Existing Preview2 Application to Preview 3

The information in this section describes the changes you must make to modify an ASP.NET MVC application that was created with the Preview 2 release so that it works with the Preview 3 release.

Code Changes

·         Update the references to the following assemblies to point to the new Preview 3 versions of the assemblies:

·         System.Web.Abstractions

·         System.Web.Routing

·         System.Web.Mvc

By default, these assemblies are located in the following folder:

%ProgramFiles%/Microsoft ASP.NET/ASP.NET MVC Preview 3

·         For all existing action methods, change the return type from void to ActionResult.

·         Anywhere you call RenderView, change it to a call to return View. You can search for RenderView( and replace it with return View(.

·         Anywhere you call RedirectToAction, prepend the call with the return keyword. Search for RedirectToAction( and replace it with return RedirectToAction(.

·         If you use a strongly typed page, replace <%= ViewData.PropertyName %> with <%= ViewData.Model.PropertyName %>. Rather than replacing the ViewData object with your strongly typed object, the MVC framework now sets the Model property to the instance that you provide.

·         In the Global.asax file, remove the route definition for Default.aspx. In the default Preview 2 template, the route looked like the following example:

routes.Add(new Route("Default.aspx", new MvcRouteHandler())

{

  Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),

});

·         In the Global.asax file, find the following default MVC route:

routes.Add(new Route("{controller}/{action}/{id}", new MvcRouteHandler())

{

  Defaults = new RouteValueDictionary(new { action = "Index", id = "" }),

});

Replace it with the following route:

routes.MapRoute(

    "Default",                                      // Route name

    "{controller}/{action}/{id}",                   // URL with parameters

    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

);

·         Add the following line at the very beginning of the RegisterRoutes method:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

·         Edit the Default.aspx file and add the following line:

<% Response.Redirect("~/Home") %>

This redirect is not necessary for IIS 7. This is a workaround for an issue with how the Web server that is built into Visual Studio (the ASP.NET Development Server) works with routing.

Configuration Changes

·         In the Web.config file, you must change the type attribute of the httpHandler entry in the <system.webServer> section for UrlRoutingHandler to System.Web.HttpNotFoundHandler. To do this, search for the following string in the file:

path="UrlRouting.axd" type="System.Web.Routing.UrlRoutingHandler, System.Web.Routing, Version= 3.5.0 .0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"

Replace it with the following string:

path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web, Version= 2.0.0 .0, Culture=neutral, PublicKeyToken=b 03f 5f 7f 11d 50a 3a "

·         Because the version numbers of the System.Web.Abstractions and System.Web.Routing assemblies have been changed to 0.0.0 .0, you must update version information in the Web.config file. In the Web.config file, search for the following string:

System.Web.Routing, Version= 3.5.0 .0

Replace it with the following string:

System.Web.Routing, Version= 0.0.0 .0

Search for the following string:

System.Web.Abstractions, Version= 3.5.0 .0

Replace it with the following string:

System.Web.Abstractions, Version= 0.0.0 .0

Integrating with ASP.NET Dynamic Data

If you use ASP.NET MVC and Dynamic Data together in the same Web application, you will run into a problem because the assembly versions for System.Web.Routing and System.Web.Abstractions are not the same. If you are running the latest preview of Dynamic Data from the Code Gallery Web site (http://code.msdn.microsoft.com/dynamicdata), you can get this working with MVC in the same project. You will need to add the following assembly binding redirects in the Web.config file.

<runtime>

  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

    <dependentAssembly>

      <assemblyIdentity name="System.Web.Routing"

          publicKeyToken="31bf3856ad364e35"/>

      <bindingRedirect oldVersion=" 0.0.0 .0" newVersion="3.5.0.0"/>

    </dependentAssembly>

    <dependentAssembly>

      <assemblyIdentity name="System.Web.Abstractions" publicKeyToken="31bf3856ad364e35"/>

      <bindingRedirect oldVersion=" 0.0.0 .0" newVersion="3.5.0.0"/>

    </dependentAssembly>

   </assemblyBinding>

 </runtime>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值