-1
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 class

{
    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; }     
}

get method

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

body api is:

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

I want the method to return me only this data:

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

In adressInfo, I concatenate the strings Address, City, Region, Postal Code and Country, the single strings, I need to declare them for reverseMap for the post method.

Someone who gives me a tip?

Sorry for my English, is not sufficient.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 2
    Does this answer your question? [Hide properties from web api response](https://stackoverflow.com/questions/55805578/hide-properties-from-web-api-response) – Bruno Xavier Jul 21 '21 at 14:31

1 Answers1

1

try this

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();
        }}
Serge
  • 40,935
  • 4
  • 18
  • 45