第一次接触到web api,发现这个东西是REST风格的:----


        微软的web api是在vs2012上的mvc4项目绑定发行的。它提出的web api是全然基于RESTful标准的,全然不同于之前的(同是SOAP协议的)wcf和webService,它是简单。代码可读性强的。上手快的,假设要拿它和web服务相比,我会说,它的接口更标准。更清晰,没有混乱的方法名称。有的仅仅有几种标准的请求,如get,post,put,delete等,它们分别相应的几个操作。以下讲一下:


        GET:生到数据列表(默认),或者得到一条实体数据

        POST:加入服务端加入一条记录,记录实体为Form对象

        PUT:加入或改动服务端的一条记录,记录实体的Form对象,记录主键以GET方式进行传输

        DELETE:删除 服务端的一条记录


        由于之前看一个hybird app的接口设计,发现就是这样的风格的,貌似用的非常多,作为小白的我就心里mark了一下,希望能在自己项目组的hybird app的项目中用到。


       以下来写个简单web api的样例,来看下web api的使用及获取client的信息:


        1,首先建立一个web api的项目。

        2,加入controller跟model:


                  


       user类:

             

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

namespace BrowerTest.Models
{
    public class User
    {
        public int Id { get; set; }
        public String UName { get; set; }
        public int UAge { get; set; }
        public String UAddress { get; set; }

    }
}

    Controller类:


           

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



namespace BrowerTest.Controllers
{
    public class UserController : ApiController
    {
       

        public List<BrowerTest.Models.User> GetUser(HttpRequestMessage httpreq)
            
        {
           
            //返回数据
            var userList = new List<BrowerTest.Models.User> { 
            new BrowerTest.Models.User{ Id=1,UName="张三",UAge=12,UAddress="海淀区"},
            new BrowerTest.Models.User{Id=2,UName="李四",UAge=23,UAddress="昌平区"},
            new BrowerTest.Models.User{Id=3,UName="王五",UAge=34,UAddress="朝阳区"}
            };

            var temp = (from u in userList
                        select u).ToList();
            return temp;
        }
    }
}

        之后,run下看看,由于我这里未定义路由,所以使用默认路由,訪问地址为:http://****/api/controllerName/actionName  。

比如:http://localhost:12333/api/user/getuser。


      3,通过HttpRequestMessage获取请求信息


           

  public class UserController : ApiController
    {
        /*
         * 获取客户端IP地址--------------
         方法说明: this version will return a string with the client IP. If it returns ::1 that means the client is requesting from the same computer as where the API is running. The query address will be something like http://yoursite/api/ip depending on your routing.
         * (此方法会返回一个IP字符。假设为“::1”表示客户端的请求来自跟api接口同样的PC机上面。

) * 方法来源:http://www.herlitz.nu/2013/06/27/getting-the-client-ip-via-asp-net-web-api/ (国外站点,可能须要FQ才干够呀。在stackoverflow上看到的) */ private string GetClientIp(HttpRequestMessage request = null) { request = request ?

?

Request; if (request.Properties.ContainsKey("MS_HttpContext")) { return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress; } else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name)) { RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]; return prop.Address; } else if (HttpContext.Current != null) { return HttpContext.Current.Request.UserHostAddress; } else { return null; } } public List<BrowerTest.Models.User> GetUser(HttpRequestMessage httpreq) { /*读取客户端信息*/ httpreq.GetUrlHelper().ToString(); String url=httpreq.RequestUri.ToString();//当前页面地址 String userAagent = httpreq.Headers.UserAgent.ToString();//User-Agent所有信息:里面包括浏览器版本号跟操作系统版本号 String ClientIp = GetClientIp(httpreq);//客户端IP地址 //返回数据 var userList = new List<BrowerTest.Models.User> { new BrowerTest.Models.User{ Id=1,UName="张三",UAge=12,UAddress="海淀区"}, new BrowerTest.Models.User{Id=2,UName="李四",UAge=23,UAddress="昌平区"}, new BrowerTest.Models.User{Id=3,UName="王五",UAge=34,UAddress="朝阳区"} }; var temp = (from u in userList select u).ToList(); return temp; } }


       

       感觉用起来跟Asp.net 的controller差点儿相同,挺适合做接口的,还没深入研究内部原理,mark下,有空用spring mvc做个java版的。