【问题标题】:Hide parameter in API body在 API 正文中隐藏参数
【发布时间】:2021-09-28 22:24:55
【问题描述】:
public partial class Employee
{
    public int EmployeeId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Title { get; set; }
    
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }    
}

dto类

{
    public int EmployeeId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Title { get; set; }
       
    public string infoAdress{get; set;}
        
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }     
}

获取方法

public async Task<ActionResult<IEnumerable<EmployeeDto>>> GetEmployees()
{
    return await _context.Employees
        .ProjectTo<EmployeeDto>(_imapper.ConfigurationProvider).ToListAsync();
}

body api 是:

{
    "employeeId": 0,
    "lastName": "string",
    "firstName": "string",
    "adressInfo": "string",
    "address": "string",
    "city": "string",
    "region": "string",
    "postalCode": "string",
    "country": "string"
  }

我希望该方法只返回这些数据:

{
    "employeeId": 0,
    "lastName": "string",
    "firstName": "string",
    "adressInfo": "string",
}

在 adressInfo 中,我将字符串 Address、City、Region、Postal Code 和 Country 连接起来,单个字符串,我需要为 post 方法的 reverseMap 声明它们。

有人给我小费吗?

对不起,我的英语还不够。

【问题讨论】:

标签: c# api swagger


【解决方案1】:

试试这个

public async Task<ActionResult<IEnumerable<EmployeeDto>>> GetEmployees()
        {

          return await _context.Employees
                     .Select(i=> new EmployeeDto
                     { 
                        employeeId=i.EmployeeId,
                        lastName=i.LastName,
                        firstName=i.FirstName,
                        adressInfo=i.Address
                      }
                    .ToListAsync();
        }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 2012-05-12
    相关资源
    最近更新 更多