【发布时间】:2019-09-23 16:41:25
【问题描述】:
我正在使用 ASP.Net Core 2.2 制作一个小型 Web 应用程序。
目前我只有一个界面:
public interface ILocation {
public int Type {get;}
}
有2个类实现了这个ILocation接口:
public class StreetLocation: ILocation {
public int Type {get;set;}
public string City {get;set;}
public string State {get;set;}
}
public class GeoCoordinateLocation: ILocation {
public int Type {get;set;}
public double Latitude{get;set;}
public double Longitude{get;set;}
}
我有一个类StudentViewModel,用于接收从客户端发送到服务器的信息。该类实现如下:
public class StudentViewModel {
public Guid Id {get;set;}
public string Name {get;set;}
public ILocation Address {get;set;}
}
我从客户端发出的请求是这样的:
{
"id": "0da28089-c0da-41a7-a47f-89b54d52822b",
"name": "Student 01",
"location": {
"type": 1,
...
}
}
如果location type为1,ILocation将反序列化为StreetLocation,如果location type为2则反序列化为GeoCoordinateLocation。
请问有什么解决办法吗?
IModelBinder 在 asp.net core 中可以做到吗?
谢谢
【问题讨论】:
标签: c# asp.net-core asp.net-core-webapi asp.net-core-2.1