.Net Web Api——獲取客戶端瀏覽器資訊
第一次接觸到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/ (國外網站,可能需要翻牆才可以呀,在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版的。