首页 > 解决方案 > 如何在 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 时会隐藏响应正文?

标签: c#asp.net-core

解决方案


推荐阅读