Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

// GET: api/Events/5
        [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:

SQL
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"?
Posted

1 solution

It returns Id because you're returning EventDo object which has
C#
public int Id { get; set; } 

defined. It is zero because you're not setting it here
C#
.Select(e =>
                new EventDto()
                {}


and default for integer variable is zero.



If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
varmartins 9-Feb-15 7:52am    
It helps regarding know why and what is happening but still not sure how to fix this... what I want is for the id to not show in the get request, but I think I need the Id field in the DTO to use as a "where" in a select right?
Sinisa Hajnal 9-Feb-15 8:50am    
You can't have a pie and eat it. What you could (maybe) do is see if there is a way to override Ok(events) mappings so that the end user doesn't get the property (or maybe there is an attribute that marks it as private...At the very least, mark it as read only so that your user cannot change it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900