.NET 6最小API-返回接口
我有麻烦的是返回新的最小API接口。 让我们假设以下简单方案:
interface ICar {
public string PropA {get; set; }
}
class CarExample : ICar {
public string PropA {get; set; }
public string PropB {get; set; }
}
app.MapGet("/car", async ([FromServices] ICarRepository carRepository, int carId)
{
var car = await carRepository.GetCarByIdAsync(carId);
return car;
})
carrespository返回ICAR。 不幸的是,JSON结果没有可能。 铸造不是一种选择,因为我不知道确切的汽车。
I have troubles to return an interface with the new minimal Api.
Lets assume the follow simple scenario:
interface ICar {
public string PropA {get; set; }
}
class CarExample : ICar {
public string PropA {get; set; }
public string PropB {get; set; }
}
app.MapGet("/car", async ([FromServices] ICarRepository carRepository, int carId)
{
var car = await carRepository.GetCarByIdAsync(carId);
return car;
})
The carRespository returns ICar as Result.
Unfortunately the Json Result has no ProbB.
Casting is not an option, because I do not know the exact Car.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个选项是将
CAR
铸造为对象
:我非常确定它是由于
system.text.json
在序列化过程中处理通用物的原因而发生的:请参阅关于多型销售的文档。
One option is to cast
car
toobject
:I pretty much sure it happens due to how
System.Text.Json
handles generics during serialization:See the docs on polymorphic serialization.