CuriousDeveloper
CuriousDeveloper

Reputation: 899

Web API not parsing get parameters correctly

I have the following url

    http://localhost/api/map/tmc/identify?
    geometry={x:-112.0469856262207,y:33.3926093953406, spatialReference:{wkid:4326}}
    &geometryType=esriGeometryPoint
    &mapExtent={xmin:-112.18062400817871,ymin:33.33956359362892,xmax:-111.95076942443848,ymax:33.49201883920683, spatialReference:{wkid:4326}}
    &tolerance=5
    &sr=4326
    &imageDisplay=1340,1065,96
    &layers=all:0
    &returnGeometry=true
    &returnM=false

I am trying to intercept that object using the following action

    public class SpatialReference
    {
        public int wkid { get; set; }
    }

    public class Geometry
    {
        public double x { get; set; }
        public double y { get; set; }
        public SpatialReference spatialReference { get; set; }
    }

    public class MapExtent
    {
        public double xmin { get; set; }
        public double ymin { get; set; }
        public double xmax { get; set; }
        public double ymax { get; set; }
        public SpatialReference spatialReference { get; set; }
    }

    public class RootObject
    {
        public Geometry geometry { get; set; }
        public string geometryType { get; set; }
        public MapExtent mapExtent { get; set; }
        public int tolerance { get; set; }
        public int sr { get; set; }
        public List<int> imageDisplay { get; set; }
        public string layers { get; set; }
        public bool returnGeometry { get; set; }
        public bool returnM { get; set; }
    }

    [HttpGet]
    [Route("api/map/tmc/identify")]
    public object Identify([FromUri]RootObject root)
    {
        return root;
    }

And I get back

{  
   "geometry":{  
      "x":0.0,
      "y":0.0,
      "spatialReference":null
   },
   "geometryType":"esriGeometryPoint",
   "mapExtent":{  
      "xmin":0.0,
      "ymin":0.0,
      "xmax":0.0,
      "ymax":0.0,
      "spatialReference":null
   },
   "tolerance":5,
   "sr":4326,
   "imageDisplay":[  
      0
   ],
   "layers":"all:0",
   "returnGeometry":true,
   "returnM":false
}

as you can see the tolerance and sr were set correctly but the objects were not. Unfortunately I have no control over the request (It's always a GET and in this format). How can I correctly parse the url into the right objects

Upvotes: 1

Views: 670

Answers (1)

degant
degant

Reputation: 4981

Since you have 9 query parameters in your http GET, you should declare 9 arguments in the action method instead of a root object i.e:

[HttpGet]
[Route("api/map/tmc/identify")]
public object Identify([FromUri]Geometry geometry, 
    [FromUri]string geometryType, 
    [FromUri] MapExtent mapExtent,
    ...)

[FromUri]RootObject root will not map the parameters correctly since they are query parameters and not a POST body

Upvotes: 2

Related Questions