WCF Web API 說再見,繼承者ASP.NET Web API


從 .NET 3.5 開始 WCF 已經支持用 WebHttpBinding 構建 RESTful Web 服務,基於 WCF 框架的 RESTful Web 服務還是建立在 WCF Message 棧上,還是基於RPC風格的,因為 REST 的工作原理有所不同,它不需要依賴 SOAP 協議,因此 WCF 消息管道對於它經過了特殊的消息優化。但 REST 集成在 WCF 消息管道上還是不理想,所以微軟重新開始構造基於Http 協議特點的RESTful的Web API, 從2010年10月份開始把代碼放在codeplex上http://wcf.codeplex.com/ ,我也一直在跟蹤,學習WCF Web API, 上個月上掛出了一個聲明:

image 

具體內容可以參看 WCF Web API is now ASP.NET Web API。幾個月之前WCF和ASP.NET 團隊合並,把WCF Web API的內容並入了ASP.NET Web API,目前WCF Web API的所有功能並沒有完成移植,將在ASP.NET Web API正式發布的時候完成移植,非常期待正式發布的ASP.NET  Web API,更期望ASP.NET MVC 4會和ASP.NET MVC 3一樣可以很好的運行在Mono上,這樣Mono平台就完美了,可以完全的支持RESTful風格的API。

同時提供了一份WCF Web Api到ASP.NET Web API的遷移指南How to Migrate from WCF Web API to ASP.NET Web API,其中列出了 WCF Web Api 到 ASP.NET Web Api 的映射表:

WCF Web API ASP.NET Web API
Service Web API controller
Operation Action
Service contract Not applicable
Endpoint Not applicable
URI templates ASP.NET Routing
Message handlers Same
Formatters Same
Operation handlers Filters, model binders

ASP.NET Web API構建於ASP.NET引擎之上和共享了許多ASP.NET MVC的特性,例如他完全支持MVC風格的Routes和Filters,Filters在授權和異常處理方面是特別有用。Web API支持Model Binding和驗證(.NET4.5的WebForm也支持哦)。Web API框架內部自動支持XML和JSON格式,用戶可以自行開發其他類型的超媒體類型。

讓Web API的返回值變成IQueryable<T>,Web API會自動啟用OData query conventions

ASP.NET Web API還有一個特性就是可以類似於WCF自宿主方式部署,當然也可以在IIS上運行。

下面來看看如何使用 ASP.NET Web Api (使用的是 VS2010版)

image

創建出的工程中,Controllers 目錄下會有一個 ValuesController.cs 注意它繼承於 ApiController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;

namespace MvcApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        // GET /api/values
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET /api/values/5
        public string Get(int id)
        {
            return "value";
        }

        // POST /api/values
        public void Post(string value)
        {
        }

        // PUT /api/values/5
        public void Put(int id, string value)
        {
        }

        // DELETE /api/values/5
        public void Delete(int id)
        {
        }
    }
}

在 Global.cs 中,注冊了 Api 的 Url Map: api/{controller}/{id} 每個"Action"是通過 Http謂詞(GET/POST/PUT/DELETE)映射的。

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

 

參考資料:

http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/16/introducing-asp-net-mvc-4-beta-with-web-apis.aspx

http://blogs.msdn.com/b/carlosfigueira/archive/tags/aspnetwebapi/

http://www.codeproject.com/Articles/344078/ASP-NET-WebAPI-Getting-Started-with-MVC4-and-WebAP

http://www.davidhayden.me/blog/asp.net-mvc-4-web-api-routes-and-apicontroller


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM