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