Reputation: 3968
I have the following (simplified) API controller:
public async Task<IHttpActionResult> GetById(int Id)
{
var res = await _service.GetById(Id);
return Ok(res);
}
and _service.GetById(Id)
is returns returnDto
.
and returnDto
is as follows:
public class returnDto
{
public int Id{get; set;}
public string value{get;set;}
public string extra{get;set;}
}
The issue is I want to exclude Id
from returnDto
when it is returned by the controller eg GetById
I know I can do this:
[DataContract]
public class returnDto
{
public int Id{get; set;}
[DataMember]
public string value{get;set;}
[DataMember]
public string extra{get;set;}
}
but this will exclude Id from all instances of this class, whereas I only want it excluded from this method.
I also know I can make a new Dto, but this seems a bit redundant to me.
Can it be done?
Upvotes: 0
Views: 169
Reputation: 12171
You can return anonymous type:
public async Task<IHttpActionResult> GetById(int Id)
{
var res = await _service.GetById(Id);
return Ok(new {value = res.value, extra = res.extra});
}
Upvotes: 3