第一次接触到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:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAV8AAAD8CAYAAADQSqd1AAAgAElEQVR4Ae2df4wc53nfH1q0iBaiatWSKEd2zL2L0O0WTGIzdZUiRnVnHejzCSg0SqSiQMOIB1I8wCcIOBxcX4z8ETDnFIs1FJ+DI3RYK0wLt3atdQ3oejCOvivsPyoRklPE9nZb4bi0ZCciJVuJSKKiLZh9nnfm3Z3Zndmd3Z3Z+fF+BzjOr/fn5x0+99yzs893303eKEPb5cs/9Yz29ctveM79Tu45dJfn8oEDt6rzN376ttrf9f7b1f7Agf2ecuM4KRaLdPDgQTp79iwdPXp05C4feughqj33jYHasR55mJ5//vk+dZpUtWapXFynxupUn7JR3N6lleIC1ZymrPUGjaXbKIaONkAgBIH3hCiT3yLyaycFv3rOnDlDp0+fppdffjm9rJs7tEklKtW2aTf2UdqGl9jgNhrys0WTaytOv/JLoEgrQw9i1PqxTx4dGEJg/K6eIWAHmeaxY8dUcTHAUXjA4slGvTV3NonmKrRYnKXt3VWaitP5bV6kRmmZKq0+CjRfW416SmgPBBIlYLbnmyh6b+digI8fP6484KtXr3pvJn7WJGV7p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Mt7qaHHHQWQE8LZDZCiHa6hSqYwcanD3LG83iPcrXq94vLLJsYQc5N63v/1td/FQxxIDbUxXSaR/Wgo/XFPk5EUAyLuJp9lfDsiibfZCF5yqtpSQiqr2kynydmaf9arDYZKJDfZ4W12xvJHqKFgOST5gs+O8BZYwqtDyJssmrUxQA3FfP/q4NiQBo2WErrzx9wrb3Xf9I7XPg4yQ/sBNYrxyLJsY3u9///v0xS9+UR2ri/gHBEAgUQKGG9+3Ffy777pd7fNgfGUiYnTF4IoHLJt4vPqauoB/QAAEEicA48tLkDfjm/hThQGAAAj0JYAP3PoiQgEQAAEQiJ6A2R+4jfjKa/TLgRZBAARMIQDP15SVxjxBAARSRcBozxeOb6qeRQwGBIwiYLTxzftKQ0Io7yuM+WWZAMIOWV69IccePoH6kB2gGgiAQF8CML59EWW/gE6crvcyo2QNMGcUs4pkTpZGe76SInOlyt8U1Pklsv9oYQYjEIDxHQFe1qsma4CzTi/8+FXuiiJ/rZkT/KxOh6+HkvkmgJhvjtdXvtkmGnGdKhbiAcsmyXXEAD/11FM5ppD81C7t1ak0edgeiErJOZ/8oDCCxAnA8018CaIfgORx+OY3v6ly+F6+fJka/+d/D55EPfphoUUQAAEXAbONr7xrlsP3zSQpu3i8sslezm/ZP+xS6/jssLI/3nqWqEN0bk5u3ZZUjz53ZIi8sWF3e5xSsrpCRY+Qp/t+kdz9BcoJecYTVN973aveLPecsThj1v2qDGk1orokZi+y8oeam1sBxN2u33w8g8NJjggM+z8yJwjyaX214dWLpEIPN0f7LTOc7I8YFq+kUG2+oIdl71mmpzi7SXNbrI6hUjYGy/twll3VHmsCqfhpo8EJ0jfZsrU2ub9Gk9KWSqC+RXOcDlIZbzZ6/nJCrcp8EDTe7uvd0kh1Ku/N2P3aSYBJfs9MrTZYkshOv9louNQ7VLf95uMeG47zRuD/A0cMsu/RWce4AAAAAElFTkSuQmCC" alt="" />
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/ (国外站点,可能须要翻墙才干够呀。在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版的。