【发布时间】:2019-06-05 07:02:26
【问题描述】:
我在ASP.Net Core 2.1 和EF Core 2.1 中有以下端点
public class CustomerController
{
public IActionResult Post([FromBody]CustomerTO customer)
{
}
public IActionResult Get(int id)
{
var customer = _dal.GetCustomer(id);
return Ok(customer);
}
}
CustomerTO 看起来像
public class CustomerTO
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
//others
}
现在问题出在 Swagger 文档中,POST 的请求负载包括 CustomerId: 0(但可选)
所以 API 的使用者在 POST 请求中传递 CustomerId = someInt,作为它在 EF Core Dal 中的一个 Identity 属性,它会抛出错误
Cannot insert value on Identity column...
这个错误很明显并且可以接受,
我的要求是什么我如何让 Swagger 知道 CustomerId 是 不是 POST 请求中请求负载的一部分?
为 Get & Post 创建单独的 DTO 似乎是一种开销。
谢谢!!
【问题讨论】:
-
如果您可以使用
[JsonIgnore]属性,它应该开箱即用,否则您可以在此thread 中找到解决方案 -
@DarjanBogdan,但我需要在获取响应负载中包含 CustomerId 属性
-
通常这就是将输入/请求和输出/响应模型分开的原因:)
标签: c# asp.net-core swagger asp.net-core-webapi swagger-ui