.Net Web Api——获取客户端浏览器信息

来源:互联网 发布:科普数据米兔医讯 编辑:程序博客网 时间:2024/06/06 10:05


        第一次接触到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版的。









         


1 0
原创粉丝点击