Click here to Skip to main content
15,921,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, I have an as.net web api application with this controller:

// GET: api/Events/5
        [ResponseType(typeof(EventDto))]
        public async Task<IHttpActionResult> GetEvent(int id)
        {
            var events = await db.Events.Include(e => e.Name).Select(e =>
                new EventDto()
                {
                    Id = e.Id,
                    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);
        } 


and I'm using postman chrome extension to test it with this json:

{
"Name": "psilva", 
"Project": "a",
"Objectives": "s",
"City": "a",
"Country": "s",
"EventStart": 2012,
"Departure": 2012,
"Arrival": 2012,
"Registration": "a",
"NationalTransportation": "a",
"Accommodation" : "a",
"AcNumberNights": 1,
"AcPreferHotel": "a",
"AcPreferHotelUrl": "a",
"Flight": "a",
"FlDeparture": 2012,
"FlDepartPrefer": "a",
"FlDepartPreferUrl": "a",
"FlReturn": 2012,
"FlRetPrefer": "a",
"FlRetPreferUrl": "a",
"Notes": "a",
"Files": "a",
"Status": "a"    
}


and I'm getting this on the postman:

{
    "Message": "The request entity's media type 'text/plain' is not supported for this resource.",
    "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Event' from content with media type 'text/plain'.",
    "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
    "StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}


any idea why?
Posted
Updated 22-May-18 8:51am

1 solution

That is because you need to change the media-type to,
Content-Type: application/json


for the Web API to accept and work on it. text/plain is not supported, that is what exception is telling you.
 
Share this answer
 

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