c# - 如何在 Asp.net Core web api 中返回不同的模型
问题描述
我有两个 DTO 用于一个模型类文件。我在接受 POST 请求时使用第一个 DTO。处理 POST 请求后,我返回第二个 DTO。但是当我这样做时,响应体看起来像;
Response body
[
{},
{}
]
另一方面,如果我使用一个 DTO 来获取和返回对象,我可以正确地获取响应正文。顺便说一句,我在数据库中有两条记录。
档案.cs
public class Dossier
{
public Guid Id { get; set; }
public int? OpeningTypeId { get; set; }
public int? SourceId { get; set; }
public int? InstitutionTypeId { get; set; }
public int? ResultId { get; set; }
public string LinkedUnit { get; set; }
public int? LinkedUnitId { get; set; }
public int? ComplaintSubjectId { get; set; }
public int? DossierYearId { get; set; }
public string DossierNumber { get; set; }
public string DossierName { get; set;}
public int? CityId { get; set; }
public string Description { get; set; }
public DateTime CreatedAt { get; set; }
public string CreatedBy { get; set; }
public DateTime? ModifiedAt { get; set; }
public string ModifiedBy { get; set; }
public DateTime? DeletedAt { get; set; }
public string DeletedBy { get; set; }
public bool IsInUse { get; set; }
}
CustomDossierModel.cs
public class CustomDossierModel
{
Guid DossierId { get; set; }
string DossierNumber { get; set; }
string DossierName { get; set; }
string ComplaintSubject { get; set; }
string Complainant { get; set; }
string Interlocutor { get; set; }
string LinkedUnit { get; set; }
string Source { get; set; }
string InstitutionType { get; set; }
string Status { get; set; }
}
[Route("[action]")]
[HttpPost]
public async Task<IEnumerable<CustomDossierModel>> GetFiltered([FromBody] Dossier Entity)
{
string whereClause = "";
return await unitOfWork.Dossiers.GetAllFilteredDossier(Entity);
}
当我将 CustomDossierModel 更改为 Dossier 时,一切正常。为什么我使用 CustomDossierModel 时会隐藏响应正文?
解决方案
推荐阅读
- html - HTML 输入复选框无法删除勾选
- reactjs - 反应原生水平平面列表崩溃
- android - 具有开始和结束位置的水平进度条
- mysql - MySQL 查询历史表中的跟踪记录
- html - 在 Popover 中加载局部视图
- python - 如何在 Pytorch 的 CNN 中访问卷积层的权重和 L2 范数?
- sql - SQL Where Condition 无法找到不使用子选择或变量时找到的数据
- python - ImportError:无法从“kerastuner.engine.hyperparameters”导入名称“Hyperparameters”
- sql-server - 我可以覆盖单个列的列类型映射吗
- javascript - TypeError:解析数组时 LIST.map 不是函数