zoukankan      html  css  js  c++  java
  • 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

    欢迎大家扫描下面二维码成为我的客户,为你服务和上云

  • 相关阅读:
    String类中常用的操作
    android 界面布局 很好的一篇总结[转]
    LeetCode Top 100 Liked Questions in Golang(updating...)
    春招(实习生招聘) 总结
    Tinywebserver:一个简易的web服务器
    Tinychatserver: 一个简易的命令行群聊程序
    Tinyshell: 一个简易的shell命令解释器
    LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
    LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
    剑指offer 1-6
  • 原文地址:https://www.cnblogs.com/shanyou/p/2390672.html
Copyright © 2011-2022 走看看