. Net Web api--getting client browser information

Source: Internet
Author: User


The first time I touched the Web API, I found this thing to be restful:----


Microsoft's Web API is published on the MVC4 project bindings on vs2012, and its proposed Web API is completely based on the restful standard, completely different from the previous (as is the SOAP protocol) WCF and WebService, it is simple, code readable, quick to get started, If I want to compare it with the Web service, I would say that its interface is more standard, clearer, no confusing method name, some only a few standard requests, such as Get,post,put,delete, they correspond to several operations, the following:


Get: Raw to Data list (default), or get an Entity data

POST: Add a Service side to add a record that records the entity as a Form object

PUT: Add or modify a record on the service side, record the entity's Form object, and record the primary key to be transferred in a Get mode

Delete: Deletes a record on the service side


Because before looking at a Hybird app interface design, found that this style, seems to use a lot, as a small white I was in the mind mark a bit, I hope to be in the project group of the Hybird app project.


Here's an example of a simple Web API that looks at the use of the Web API and gets the client's information:


1, first build a Web API project;

2, add controller and model:



User class:

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 class:


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) {//return data var userlist = n            EW list<browertest.models.user> {new browertest.models.user{id=1,uname= "Zhang San", uage=12,uaddress= "Haidian"}, New Browertest.models.user{id=2,uname= "John Doe", uage=23,uaddress= "Changping District"}, New Browertest.models.user{id=3,u            Name= "Harry", uage=34,uaddress= "Chaoyang District"}; var temp = (from u in userlist select u).            ToList ();        return temp; }    }}

After that, run and look at it because I don't have a route defined here, so using the default route, the access address is: Http://****/api/controllername/actionname. For example: Http://localhost:12333/api/user/getuser.


3, GET request information via Httprequestmessage


  public class Usercontroller:apicontroller {/* * Get Client IP Address--------------method Description: This version W Ill return a string with the client IP. If it returns:: 1 That means the client was requesting from the same computer as where the API is running.         The query address would be something as HTTP://YOURSITE/API/IP depending on your routing. * (This method returns an IP character; if ":: 1" indicates that the client's request comes from the same PC as the API interface.) * Method Source: http://www.herlitz.nu/2013/06/27/getting-the-client-ip-via-asp-net-web-api/(foreign website, may need to turn over the wall can be ah, in Stackoverf  (See on low) */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.Requ Est.                userhostaddress;                } else {return null;                    }} public list<browertest.models.user> GetUser (Httprequestmessage httpreq) {/* Read client information */Httpreq. Geturlhelper ().            ToString (); String Url=httpreq. Requesturi.tostring ();//Current page address String useraagent = httpreq.             Headers.UserAgent.ToString ();//user-agent All information: contains the browser version and the operating system version of String ClientIP = Getclientip (httpreq);//Client IP address return data var userlist = new List<browertest.models.user> {new BrowerTest.Models.Us er{id=1, Uname= "Zhang San", uage=12,uaddress= "Haidian"}, New Browertest.models.user{id=2,uname= "John Doe", uage=23,uaddress= "Changping District"},            New Browertest.models.user{id=3,uname= "Harry", uage=34,uaddress= "Chaoyang"}; var temp = (from u in userlist select u).            ToList ();        return temp; }    }

The feeling is similar to the controller of the ASP., it is suitable for the interface, not in-depth study of the internal principle, mark, there is a time to use Spring MVC Java version.










. Net Web api--getting client browser information

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.