[ASP.NET] Web API 2.1 RC New Future

  • 3120
  • 0

[ASP.NET] Web API 2.1 RC New Future

在美國時間12/9日微軟釋出了ASP.NET MVC 5.1, Web API 2.1 and Web Page 3.1. 的候選版本,使用者可以透過 NuGet 取得安裝,不過您必須先確認是否符合以下條件

  • Visual Studio 2012的使用者必須有安裝 "ASP.NET and Web Tools 2013.1 for Visual Studio 2012 "
  • Visual Studio 2013的使用者必須有安裝 " Visual Studio 2013 Update 1 RC "

這個 RC 版的釋出,我們可以使用Visual Studio Visual Studio 的 NuGet 主控台來安裝,如下所示(以Visural Studio 2013為例)

Install-Package Microsoft.AspNet.WebApi -Version 5.1.0-rc1 –Pre

image

 

如果您有無法順利在VS2013中文版環境下安裝成功的問題,可以參考另一篇文章的解決方法 Web API 2.1 RC1 在中文版Web API 2.0專案下無法升級安裝

 

接著讓我們來快速看一下,從目前官方提供的訊息針對Web API 在這個 RC版的一些 New Future( 目前仍是 RC ,但根據以往經驗跟最後正式版通常不會差別太多 )

 

(1) 首先是Global Error Handling的部份

透過繼承ExceptionLogger、ExceptionHandler (位於System.Web.Http.ExceptionHandling Namespace下),讓開發者可以在一些以往無法以 ExceptionFilters Catch 到的 Exception ,像是由路由引發的Exception 、Controller Constructors Exception ,最後能被適當被記錄並自訂回應訊息。細部實作可以參考這份文件 : http://aspnetwebstack.codeplex.com/wikipage?title=Global%20Error%20Handling&referringTitle=Specs

SNAGHTML66babd2c

SNAGHTML66bbb48c

 

image

image

 

(2) Attribute Routing

屬性路由是在Web API 2 時開始提供,而現在更可以透過 IDirectRouteProvider interface、RouteProviderAttribute class 、IRoutePrefix interface、RoutePrefixAttribute class ,擴展自定屬性路由,在(https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/RoutingConstraintsSample/ReadMe.txt) 提供了一個實作範例,這個範例中示範了利用擴展自定屬性路由達到Wep API 版本區分的效果,在 RoutingConstraintsSample專案裡可以看到 VersionConstraint.cs & VersionedRoute.cs 分別繼承IHttpRouteConstraint、RouteProviderAttribute,邏輯是借由Client端端送來的 Customize Header "x-api-version" 比對屬性路由來決定由哪一個API Action 進行回應。

SNAGHTML668dfa4f

image

 

SNAGHTML668edf02

image

 

image

 

image

 

image

 

(3) BSON media type formatter

支援 BSON 格式,BSON格式可以看做是一種binary 格式的JSON格式,同樣是以Key/Value型式存在,差別在於它是binary format,關於BSON 詳細可以參考 : http://bsonspec.org/#/specification

 

(4) Support for Async filters

支援非同步的過濾器,實作上透過 override On*Async 就可以實現,AuthorizationFilterAttribute, ActionFilterAttribute, and ExceptionFilterAttribute 均已支援。

   1: public class AsyncLoggingFilter : ActionFilterAttribute
   2:     {
   3:         public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
   4:         {
   5:             await Trace.WriteAsync("Executing action named {0} for request {1}.", 
   6:                 actionContext.ActionDescriptor.ActionName, 
   7:                 actionContext.Request.GetCorrelationId());
   8:         }
   9:     }

( ref : http://www.asp.net/visual-studio/overview/2013/aspnet-and-web-tools-20132-preview-for-visual-studio-2013-release-notes#webapiglobalerror)

 

(5) Query parsing support for the client formatting library

支援Client Application 也可以輕鬆解析及更新 Query String 。( PS : 目前筆者測試似乎在RC還無法使用 )

SNAGHTML66a5705a

( ref : http://www.asp.net/visual-studio/overview/2013/aspnet-and-web-tools-20132-preview-for-visual-studio-2013-release-notes#webapiglobalerror)

 

(6) Help Page Improvements

Documentation of individual properties of parameters or return types of actions.
Documentation of data model annotations
User interface design change to accommodate the above changes.

簡單來說就是提供更詳盡的資訊,例如複雜型別會提供其成員資訊

SNAGHTML66abf27d

 

(7) IgnoreRoute support

HttpRoutesCollection 支援排除路由,可以讓我們定義比對規則去排除,這可以讓我們管理路由更輕鬆。

image

( ref : http://www.asp.net/visual-studio/overview/2013/aspnet-and-web-tools-20132-preview-for-visual-studio-2013-release-notes#webapiglobalerror)

 

 

 

Ref :

Web API Global Error Handling

ASP.NET and Web Tools 2013.2 Preview for Visual Studio 2013 Release Notes

Release Candidates for ASP.NET MVC 5.1, Web API 2.1 and Web Page 3.1.

 

若本文對您有所幫助,歡迎轉貼,但請在加註【轉貼】及來源出處,並在附上本篇的超連結,感恩您的配合囉。

By No.18