Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to deserialize json to geometry data type

$
0
0

Hi every one , I must create an api services that the user can post an geometry (location) and retrieve it.

I use PostgreSql as my database and Postgis extension. But now to make it simple I just created a sample app which my controller have a parameter type geometry and try to post it.

First i installed a nugget for NTS 

Install-Package Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite -Version 3.1.2

 Here are my sample model

using NetTopologySuite.Geometries;

namespace GeoLocationAPI.Models
{
    public class GeoLocation
    {
        public string Name { get; set; }

        public Geometry Location  { get; set; }

    }
}

And here are my controller

using GeoLocationAPI.Models;
using Microsoft.AspNetCore.Mvc;

namespace GeoLocationAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {

        [HttpPost]
        public IActionResult AddLocation(GeoLocation geoLocation)
        {

            return Ok();     
        }
    }
}

My swagger look like accepting the geometry data type

{"name": "string","location": {"userData": {},"srid": 4326
  }
}

but when i try to post a data i got an error like this

System.NotSupportedException: Deserialization of reference types without parameterless constructor is not supported. Type 'NetTopologySuite.Geometries.Geometry'


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>