Reputation: 3238
I need to return from Web Api controller some specific data that not exist in standard GET action. Let's say standard controller action is:
// GET api/xTourist/5
[ResponseType(typeof(xTourist))]
public IHttpActionResult GetxTourist(int id)
{
xTourist xtourist = db.xTourist.Find(id);
if (xtourist == null)
{
return NotFound();
}
return Ok(xtourist);
But I need to return some more data, let's say hotel name. Hotel name I got by using function:
public string FindHotelName (int id)
{
int? hotelid = db.xTourist.Find(id).КодИ;
string hotelname = db.xAdres.Find(hotelid).NameC;
return hotelname;
}
But how should I joind these data and return it all together in controller answer?
Upvotes: 1
Views: 204
Reputation: 5053
So you just want the 2 results returned? Why not just create a new object which holds both the values you need?
public class TouristReturnDTO
{
public xTourist Tourist { get; set; }
public string HotelName { get; set; }
}
public IHttpActionResult GetxTourist(int id)
{
xTourist xtourist = db.xTourist.Find(id);
if (xtourist == null)
{
return NotFound();
}
string hotelName = FindHotelName(id)
return Ok(new TouristReturnDto
{
Tourist = xtourist,
HotelName = hotelName
}
);
}
Don't even need to create the TouristReturnDTO, could just use an anonymous type if you want:
return Ok(new
{
Tourist = xtourist,
HotelName = hotelName
}
);
Upvotes: 1