Hey guys, I'm building an asp.net web api and I've been using this guide as a reference
http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5[
^]
What I want to do is when someone does a GET to this api they get all the info except the Id(which is auto-incremented). For this purpose I have this controller:
[ResponseType(typeof(EventDto))]
public async Task<IHttpActionResult> GetEvent(int id)
{
var events = await db.Events.Include(e => e.Id).Select(e =>
new EventDto()
{
Name = e.Name,
Project = e.Project,
Objectives = e.Objectives,
City = e.City,
Country = e.Country,
EventStart = e.EventStart,
Departure = e.Departure,
Arrival = e.Arrival,
Registration = e.Registration,
NationalTransportation = e.NationalTransportation,
Accommodation = e.Accommodation,
AcNumberNights = e.AcNumberNights,
AcPreferHotel = e.AcPreferHotel,
AcPreferHotelUrl = e.AcPreferHotelUrl,
Flight = e.Flight,
FlDeparture = e.FlDeparture,
FlDepartPrefer = e.FlDepartPrefer,
FlDepartPreferUrl = e.FlDepartPreferUrl,
FlReturn = e.FlReturn,
FlRetPrefer = e.FlRetPrefer,
FlRetPreferUrl = e.FlRetPreferUrl,
Notes = e.Notes,
Files = e.Files,
Status = e.Status
}).SingleOrDefaultAsync(e => e.Id == id);
if (events == null)
{
return NotFound();
}
return Ok(events);
}
Which is using a DTO that from what I understood is how I control what is returned in the GET
and I have this DTO:
public class EventDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Project { get; set; }
public string Objectives { get; set; }
public string City { get; set; }
public string Country { get; set; }
public DateTime EventStart { get; set; }
public DateTime Departure { get; set; }
public DateTime Arrival { get; set; }
public string Registration { get; set; }
public string NationalTransportation { get; set; }
public string Accommodation { get; set; }
public int AcNumberNights { get; set; }
public string AcPreferHotel { get; set; }
public string AcPreferHotelUrl { get; set; }
public string Flight { get; set; }
public DateTime FlDeparture { get; set; }
public string FlDepartPrefer { get; set; }
public string FlDepartPreferUrl { get; set; }
public DateTime FlReturn { get; set; }
public string FlRetPrefer { get; set; }
public string FlRetPreferUrl { get; set; }
public string Notes { get; set; }
public string Files { get; set; }
public string Status { get; set; }
}
What is happenning is that I'm using postman (chrome extension to test api) and I use a GET api/Events/1 and it returns:
[
{
"Id": 0,
"Name": "psilva",
"Project": "a",
"Objectives": "s",
"City": "a",
"Country": "s",
"EventStart": "2015-02-06T11:06:23.673",
"Departure": "2015-02-06T11:06:23.673",
"Arrival": "2015-02-06T11:06:23.673",
"Registration": "a",
"NationalTransportation": "a",
"Accommodation": "a",
"AcNumberNights": 1,
"AcPreferHotel": "a",
"AcPreferHotelUrl": "a",
"Flight": "a",
"FlDeparture": "2015-02-06T11:06:23.673",
"FlDepartPrefer": "a",
"FlDepartPreferUrl": "a",
"FlReturn": "2015-02-06T11:06:23.673",
"FlRetPrefer": "a",
"FlRetPreferUrl": "a",
"Notes": "a",
"Files": "a",
"Status": "a"
}
]
Why is return "Id" ? and why is it "0"?