-
Notifications
You must be signed in to change notification settings - Fork 46
Using NTS.IO.GeoJSON with ASP.NET Core MVC
Joe Amenta edited this page Jun 17, 2020
·
2 revisions
This is a solution that HarelM and Buzzology contributed.
- Add Newtonsoft.Json package reference
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
- Add the following code to the
ConfigureServices(IServiceCollection)
function of your startup class.
services.AddControllers(options => {
// Prevent the following exception: 'This method does not support GeometryCollection arguments'
// See: https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/585
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Point)));
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Coordinate)));
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(LineString)));
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(MultiLineString)));
}).AddNewtonsoftJson(options => {
foreach (var converter in NetTopologySuite.IO.GeoJsonSerializer.Create(new GeometryFactory(new PrecisionModel(), 4326)).Converters) {
options.SerializerSettings.Converters.Add(converter);
}
}).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
An example of the code in use can be seen in the IsrealHikingMap repository.
This is how jochenjonc does it in a ASP.Net Core Web Api project, tomek14 contributed.
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
// Prevent the following exception: 'This method does not support GeometryCollection arguments'
// See: https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/585
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Point)));
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(LineString)));
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(MultiLineString)));
// Addition from user tomek14
options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Feature)));
})
.AddJsonOptions(options =>
{
foreach (var converter in GeoJsonSerializer.Create(new GeometryFactory(new PrecisionModel(), 4326)).Converters)
{
options.SerializerSettings.Converters.Add(converter);
}
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// ...
}
// ...
}
Derived from Issue #5