ASP. NET Web API Data binding

Source: Internet
Author: User

1, fake with the following API, incoming latitude and longitude to obtain city information, API can be defined as api/geodata?latitude=47.678558&longitude=-122.130989 below I try to submit the latitude and longitude information as a parameter.

api/geodata?location=47.678558,-122.130989

First of all, we must think that we can get the location in the API to parse the location, this method is not recommended, below we try another method direct variable receive.

[TypeConverter (typeof(Geopointconverter))]//Marker type Converters     Public classGeoPoint { Public DoubleLatitude {Get;Set; }  Public DoubleLongitude {Get;Set; }  Public Static BOOLTryParse (stringS outGeoPoint result) {Result=NULL; varParts = S.split (','); if(Parts. Length! =2)            {                return false; }            Doublelatitude, longitude; if(Double. TryParse (parts[0], outLatitude) &&Double. TryParse (parts[1], outlongitude)) {Result=NewGeoPoint () {longitude = longitude, Latitude =latitude}; return true; }            return false; }    }    //Type Converters    classGeopointconverter:typeconverter { Public Override BOOLCanConvertFrom (ITypeDescriptorContext context, Type sourcetype) {//Check if you can convert            if(SourceType = =typeof(string))            {                return true; }            return Base.        CanConvertFrom (context, sourcetype); }         Public Override ObjectConvertFrom (ITypeDescriptorContext context, CultureInfo culture,Objectvalue) {//making data type conversions            if(Value is string) {GeoPoint point; if(Geopoint.tryparse (string) value, outPoint )) {                    returnPoint ; }            }            return Base.        ConvertFrom (context, culture, value); }    }

The above includes an entity class and a type conversion class, which is the responsibility of the type conversion class to convert the data of type string into Geopoint.

The action is defined as follows:

        [Route ("geodata")]          Public ihttpactionresult getgeodata (GeoPoint GeoPoint)        {            return  Ok (geoPoint);        }

ASP. NET Web API Data binding

Related Article

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.